ftruncate on file opened with fopen

前端 未结 3 1492
无人及你
无人及你 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:33

    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.

提交回复
热议问题