KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
How do I determine if a file is empty? The file is opened by a C program running on the Windows platform. I want to open a file in append mode, and, if empty, first print a header to it. // Open CSV & write header report_csv = fopen("SNR.csv", "a+"); if (!report_csv) { fprintf(stderr, "Unable to open CSV output file..."); return -1; } if (!ftell(report_csv)) { fprintf(report_csv, "Column A;Column B;Column C\n"); } // ... print data to file fclose(report_csv); I was expecting ftell to return the current file size if the file was not empty, which happens because the code above is looped. However, ftell always returns 0 and the header is printed multiple times. I know I could fopen it with r and use fseek / ftell / fclose and then fopen it again with a+ , but I think it's possible to do this without opening and closing the file multiple times.
Tags (comma-separated)
Save Edits
Cancel