Search code examples
cfilestat

Do I need to fflush or close a file before calling stat on it?


After opening a file for writing:

FILE *file = fopen("./file", "w");

Can I assume the file was created immediately? Is it safe to call:

 stat( "./file", info);

Or should I better:

 fflush(file);

or

 fclose(file);

beforehand?

Edit: Assume non NULL file after fopen call


Solution

  • The fopen manual page says:

    If mode is w, wb, a, ab, w+, wb+, w+b, a+, ab+, or a+b, and the file did not previously exist, upon successful completion, the fopen() function shall mark for update the st_atime, st_ctime, and st_mtime fields of the file and the st_ctime and st_mtime fields of the parent directory.

    So I think it is safe to stat on the file just after a successful fopen call.