ftruncate on file opened with fopen

前端 未结 3 1519
无人及你
无人及你 2021-01-20 20:14

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

3条回答
  •  孤独总比滥情好
    2021-01-20 20:29

    You should not really mix file handle and file descriptor calls like that.

    What's almost certainly happening without the fflush is that the some string is waiting in file handle buffers for delivery to the file descriptor. You then truncate the file descriptor and fclose the file handle, flushing the string, hence it shows up in the file.

    With the fflush, some string is sent to the file descriptor and then you truncate it. With no further flushing, the file stays truncated.

提交回复
热议问题