C语言fprintf函数失效求助:文件初始化写入操作无输出
Hey there, let's work through why your fprintf calls aren't writing content to your files like expected. I've looked over your code snippet, and here are the most common issues to check, plus straightforward fixes for each:
1. Standard I/O Buffering Is Holding Your Data
By default, C's standard library uses buffered I/O for file operations. When you call fprintf, the data gets stored in an in-memory buffer instead of being written directly to disk. The buffer only flushes to the file when it's full, the file is closed, or you manually trigger a flush. If your program exits before any of these happen, the data might never reach the file.
Fix:
Add fflush() right after your fprintf calls to force the buffer to write to disk immediately:
fprintf(ffitness_data,"#This file contains all the data that are function of fitness.\n"); fprintf(ffitness_data,"#Columns: f,<p>(f),<l>(f).\n\n"); fflush(ffitness_data); // Push buffer content to disk right now
Alternatively, you can disable buffering entirely when opening the file using setvbuf:
ffitness_data = fopen("fitness_data.txt","w"); if( ffitness_data == NULL){ printf("Impossible to open the fitness data file\n"); exit(1); } setvbuf(ffitness_data, NULL, _IONBF, 0); // Turn off buffering for this file
2. You're Not Checking for fprintf Errors
Your code properly checks if fopen succeeds, but it doesn't verify if fprintf actually writes the data. Even if the file is open, write operations can fail (e.g., disk full, sudden permission changes). Without error checking, you'll never know if the write itself went wrong.
Fix:
Check the return value of fprintf—it returns the number of characters written, or a negative value on failure:
int write_result = fprintf(fmeme_data, "Your meme data line here\n"); if (write_result < 0) { perror("Failed to write to meme_data.txt"); // Prints a detailed system error message fclose(fmeme_data); exit(1); }
3. Files Aren't Being Closed Properly
While exit(1) will close open files during process termination, if your program crashes unexpectedly before reaching exit, the buffer might never get flushed. Plus, leaving files open longer than needed is poor practice and can lead to resource leaks.
Fix:
Explicitly close the files once you're done writing to them:
// After all write operations for the file fclose(ffitness_data); fclose(fmeme_data);
Modified Example Code
Here's how your code might look with all these fixes applied:
#include <stdio.h> #include <stdlib.h> int main() { FILE *ffitness_data = fopen("fitness_data.txt","w"); if( ffitness_data == NULL){ printf("Impossible to open the fitness data file\n"); exit(1); }else{ if (fprintf(ffitness_data,"#This file contains all the data that are function of fitness.\n") < 0) { perror("Failed to write to fitness_data.txt"); fclose(ffitness_data); exit(1); } if (fprintf(ffitness_data,"#Columns: f,<p>(f),<l>(f).\n\n") < 0) { perror("Failed to write to fitness_data.txt"); fclose(ffitness_data); exit(1); } fflush(ffitness_data); fclose(ffitness_data); } FILE *fmeme_data = fopen("meme_data.txt","w"); if( fmeme_data == NULL){ printf("Impossible to open the meme data file\n"); exit(1); }else{ // Example write with error checking if (fprintf(fmeme_data, "Sample meme data entry\n") < 0) { perror("Failed to write to meme_data.txt"); fclose(fmeme_data); exit(1); } fflush(fmeme_data); fclose(fmeme_data); } return 0; }
Start with checking the buffering issue—it's the most likely culprit here. If you still run into problems, verify that your disk has enough space and that the user running the program has write permissions for the directory where you're creating the files.
内容的提问来源于stack exchange,提问作者GRquanti

