The fprintf() function can't persist the data to the file; must be followed by fflush() function?

后端 未结 1 783
小蘑菇
小蘑菇 2021-01-21 00:57

I am using the fopen() function open a file to write, and return a FILE pointer. I then use this pointer with fprintf(), but data doesn\'t

相关标签:
1条回答
  • 2021-01-21 01:29

    You either need to call fflush(), or just fclose() the file when you're done writing to it. fclose() will attempt to flush before closing. This behavior is a part of ISO C and POSIX so you can rely on it. See here for more information about fclose():

    Flushes a stream, and then closes the file associated with that stream. Afterwards, the function releases any buffers associated with the stream. To flush means that unwritten buffered data is written to the file, and unread buffered data is discarded.

    If you keep writing to a file and never flush or close it, the OS will start flushing data to the file "when it's ready to", after enough writes are queued up.

    0 讨论(0)
提交回复
热议问题