C fread() & fwrite()
In this page:
Writing binary blocks with fwrite()
Because it writes raw bytes rather than formatted text, fwrite() preserves the exact binary representation of data like floats or structs, unlike fprintf() which converts everything to human-readable text first.
Example: Writing binary blocks with fwrite()
#include <stdio.h>
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
printf("Wrote raw binary data");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading binary blocks with fread()
If fread() returns fewer elements than you requested, it usually means you've hit the end of the file partway through -- always check this return value rather than assuming the full requested amount was read successfully.
Example: Reading binary blocks with fread()
#include <stdio.h>
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
int result[3];
fp = fopen("data.bin", "rb");
fread(result, sizeof(int), 3, fp);
fclose(fp);
printf("%d", result[1]);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Writing structures to files
This is far more efficient than writing each member of a structure individually with separate fprintf() calls, since fwrite() copies the structure's raw memory layout to disk in a single operation.
Example: Writing structures to files
#include <stdio.h>
struct Point { int x, y; };
int main() {
struct Point p = {3, 4};
FILE *fp = fopen("point.bin", "wb");
fwrite(&p, sizeof(struct Point), 1, fp);
fclose(fp);
printf("Struct written in one operation");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading structures from files
This only works reliably if the reading program uses the exact same structure definition (including compiler, platform, and padding/alignment) as the program that wrote the file -- binary file formats are not portable across different compilers or architectures without extra care.
Example: Reading structures from files
#include <stdio.h>
struct Point { int x, y; };
int main() {
struct Point p = {3, 4};
FILE *fp = fopen("point.bin", "wb");
fwrite(&p, sizeof(struct Point), 1, fp);
fclose(fp);
struct Point result;
fp = fopen("point.bin", "rb");
fread(&result, sizeof(struct Point), 1, fp);
fclose(fp);
printf("%d %d", result.x, result.y);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Verifying read/write count
Comparing the returned count against your expected count is the only reliable way to detect a partial or failed read/write, since these functions don't otherwise raise an error the way some other I/O functions do.
Example: Verifying read/write count
#include <stdio.h>
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
size_t written = fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
printf("%zu", written);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: