Prerequisite: Structure in C
For writing in file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of struct. fwrite and fread make task easier when you want to write and read blocks of data.
- fwrite : Following is the declaration of fwrite function
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) ptr - This is pointer to array of elements to be written size - This is the size in bytes of each element to be written nmemb - This is the number of elements, each one with a size of size bytes stream - This is the pointer to a FILE object that specifies an output stream
// C program for writing// struct to file#include <stdio.h>#include <stdlib.h>#include <string.h>// a struct to read and writestructperson{intid;charfname[20];charlname[20];};intmain (){FILE*outfile;// open file for writingoutfile =fopen("person.dat","w");if(outfile == NULL){fprintf(stderr,"\nError opend file\n");exit(1);}structperson input1 = {1,"rohan","sharma"};structperson input2 = {2,"mahendra","dhoni"};// write struct to filefwrite(&input1;,sizeof(structperson), 1, outfile);fwrite(&input2;,sizeof(structperson), 1, outfile);if(fwrite!= 0)printf("contents to file written successfully !\n");elseprintf("error writing file !\n");// close filefclose(outfile);return0;}chevron_rightfilter_noneOutput:
gcc demowrite.c ./a.out contents to file written successfully!
- fread : Following is the declaration of fread function
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) ptr - This is the pointer to a block of memory with a minimum size of size*nmemb bytes. size - This is the size in bytes of each element to be read. nmemb - This is the number of elements, each one with a size of size bytes. stream - This is the pointer to a FILE object that specifies an input stream.
// C program for reading// struct from a file#include <stdio.h>#include <stdlib.h>// struct person with 3 fieldsstructperson{intid;charfname[20];charlname[20];};// Driver programintmain (){FILE*infile;structperson input;// Open person.dat for readinginfile =fopen("person.dat","r");if(infile == NULL){fprintf(stderr,"\nError opening file\n");exit(1);}// read file contents till end of filewhile(fread(&input;,sizeof(structperson), 1, infile))printf("id = %d name = %s %s\n", input.id,input.fname, input.lname);// close filefclose(infile);return0;}chevron_rightfilter_noneOutput:
gcc demoread.c ./a.out id = 1 name = rohan sharma id = 2 name = mahendra dhoni
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C program to copy contents of one file to another file
- Structure Member Alignment, Padding and Data Packing
- C | Loops & Control Structure | Question 1
- C | Loops & Control Structure | Question 2
- C | Loops & Control Structure | Question 3
- C | Loops & Control Structure | Question 4
- C | Loops & Control Structure | Question 5
- C | Loops & Control Structure | Question 6
- C | Loops & Control Structure | Question 7
- C | Structure & Union | Question 1
- C | Structure & Union | Question 2
- C | Structure & Union | Question 10
- C | Structure & Union | Question 4
- C | Loops & Control Structure | Question 8
- C | Loops & Control Structure | Question 9
- C | Loops & Control Structure | Question 10
- C | Loops & Control Structure | Question 11
- C | Structure & Union | Question 5
- C | Loops & Control Structure | Question 12
- C | Loops & Control Structure | Question 13
Improved By : KathiNotapplicable

