In addition to other answer, unless I miss something (related to binary file), "w"
flat makes fopen
truncate the file to 0 if it exists.
From man page:
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
Checked on my computer: you can run the code 10 times, you still have a file.txt
of length 4, containing int 4 (that is, on my LE machine, bytes 4, 0, 0, 0).
So, if I summarize both answers: your 1st solution doesn't work, because fseek(f, 0, SEEK_END)
is useless, since file is size 0, and therefore, writing already occurs at end (which is the same as beginning of file)
Second solution may not work, because you'd have no guarantee that file is positioned at it ends [edit: I see that this is disputed in comments tho]. So, I'd say, no, they are not equivalent. On my PC, calling ten times the 1st code creates a 4 bytes file. Calling ten times the second creates a 40 bytes file. Regardless of whether that 2nd behavior is UB or not, either way, it is different than the 1st behavior.