C fprintf() & fscanf()
In this page:
fprintf(file_pointer, "format", values);
fscanf(file_pointer, "%specifier", &variable_name);
Writing formatted text with fprintf()
This means all the same format specifiers you use with printf() (%d, %s, %f, and so on) work identically with fprintf(), just directed at a file stream instead of the console -- useful for generating human-readable log files or reports.
Example: Writing formatted text with fprintf()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "Age: %d", 25);
fclose(fp);
// Print formatted text to the screen
printf("Formatted text written to file");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading formatted text with fscanf()
fscanf() uses the same %d, %s, %f format specifiers as scanf(), and like scanf(), it requires addresses (using &) for non-string variables to know where to store what it reads.
Example: Reading formatted text with fscanf()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "%d", 42);
fclose(fp);
int value;
fp = fopen("data.txt", "r");
fscanf(fp, "%d", &value);
fclose(fp);
// Print formatted text to the screen
printf("%d", value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Working with mixed data types
For example, you might write a CSV-style line with fprintf(fp, "%s,%d,%.2f\n", name, age, gpa) and later parse it back with a matching fscanf() call using the same format string structure.
Example: Working with mixed data types
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.csv", "w");
fprintf(fp, "%s,%d,%.2f\n", "Alice", 20, 3.5);
fclose(fp);
char name[20];
int age;
float gpa;
fp = fopen("data.csv", "r");
fscanf(fp, "%[^,],%d,%f", name, &age, &gpa);
fclose(fp);
// Print formatted text to the screen
printf("%s %d %.2f", name, age, gpa);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Appending data with fprintf()
Because a mode positions the file pointer at the end before any writes happen, repeated fprintf() calls across multiple program runs build up a growing log file rather than overwriting previous entries each time.
Example: Appending data with fprintf()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("log.txt", "w");
fprintf(fp, "First entry\n");
fclose(fp);
fp = fopen("log.txt", "a");
fprintf(fp, "Second entry\n");
fclose(fp);
// Print formatted text to the screen
printf("Log grows across runs");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Handling EOF in fscanf()
fscanf() returns the number of items it successfully matched and assigned, and this drops below your expected count (or hits EOF entirely) once there's no more data left to read -- checking this is how you detect the end of variable-length input.
Example: Handling EOF in fscanf()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "5");
fclose(fp);
int value;
fp = fopen("data.txt", "r");
// Declare result and set it to fscanf(fp, "%d", &value)
int result = fscanf(fp, "%d", &value);
fclose(fp);
// Print formatted text to the screen
printf("%d", result == 1);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using a format specifier that does not match the variable type in
fscanf, such as%dfor afloat. - Not checking the return value of
fscanf, so a read failure goes unnoticed. - Forgetting to close the file, so buffered output may be lost.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: