SdFat writes weird stuff to binary on SD

One thing I'd like to introduce you to is the for statement. The while statement is meant to loop a variable number of times, until some condition becomes true. The for loop is meant to loop a fixed number of times.

There are places you are using a while loop to iterate a fixed number of times. Those would be more intuitive as for loops.

The two blocks of code do not open the file the same way.

In the first code, you open the file in setup(). Then, without closing the file, you try to open the file again in loop (read-only). Then, you write to the file (that you tried to open read-only).

The whole purpose of doing buffered writes to the file is because writing one byte at a time is slow. Writing an entire array at once is faster. You are defeating the purpose of collecting the data in an array if you then write one element of the array to the file at a time.