Platform is Ubuntu Linux on ARM. I want to write a string to a file, but I want every time to truncate the file and then write the string, i.e. no append.
I have thi
POSIX requires you to take specific actions (which ensure that no ugly side effects of buffering make your program go haywire) when switching between using a FILE stream and a file descriptor to access the same open file. This is described in XSH 2.5.1 Interaction of File Descriptors and Standard I/O Streams.
In your case, I believe it should suffice to just call fflush
before ftruncate
, like you're doing. Omitting this step, per the rules of 2.5.1, results in undefined behavior.