What is the proper method of opening a file for writing as binary file?

cs:c_language:write_and_read_a_binary_file

Return to Home page


Concepts:

Functions fwrite and fread for writing and reading a binary file.

Text:
After the writing of a vector of structures (the elements of the structure are a vector of characters containing the name of a student, a variable of type unsigned int containing the student identifier and a variable of type float containing the average value of the scores of the exams attended by the student), the program must:

  • open in write mode a file with name “out.bin”, in order to store in the file the vector of structures

  • written the file in a binary way by using the function fwrite

  • close the file

  • reopen the file for reading it

  • read the file by using the function fread and fill the vector of structures with the read data

  • print all the data contained in the vector of structures

  • close the file

Video solution:
Video with the explanation of the solution:

Solution:

binary_file_fwrite_fread.c
/* Write and read a binary file (fwrite and fread) */
 
#include 
#include 
#include 
 
#define LEN 50
#define N_STUD 2
 
typedef struct stud{
  char name[LEN+1];
  unsigned int id;
  float average;
}stud_t;
 
 
int main() {
  FILE *fp;
  stud_t student[N_STUD];
  int i;
  char row[LEN+1];
  unsigned int n_stud;
 
  strcpy(student[0].name, "Steven");
  student[0].id = 120000;
  student[0].average = 25.5;
 
  strcpy(student[1].name, "Julia");
  student[1].id = 120001;
  student[1].average = 28.5;
 
  fp = fopen("out.bin", "w"); /* Open the file for writing */
  if (fp == NULL){
    printf("Error: file out.bin cannot be opened\n");
    exit(1);
  }
 
 
  /* Write the file */
  /* NOTE 2: a binary file sometimes cannot be readable 
     correctly in a PC that it is not the one which generates it, 
     because, for instance, integer numbers can be coded with a
     different number of bytes.
  */
 
  /* Write in binary all the data contained in the structure */
  fwrite(student, sizeof(stud_t), N_STUD, fp); 
 
  /* DIMENSION OF THE GENERATED FILE
     The dimension of the generated file will be:
     n_stud*sizeof(stud_t)
     in the case of the computer used to test the program:
     2*60 = 120 byte
   */
 
  fclose(fp); /* Close the file */
 
 
 
  fp = fopen("out.bin", "r"); /* Open the file for reading */
  if (fp == NULL){
    printf("Error: file out.bin cannot be opened\n");
    exit(1);
  }
 
  /* Read the file */
  n_stud = 0;
  while( fread(&student[n_stud], sizeof(stud_t), 1, fp) == 1 ) {
    n_stud++;
  }
 
  fclose(fp); /* Close the file */
 
 
  /* Print the read records */
  for (i=0; i<n_stud; i++) {
    printf("%s %d %f\n", student[i].name, student[i].id, student[i].average);
  }
 
 
  return 0;
}


If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/write_and_read_a_binary_file

/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/write_and_read_a_binary_file.txt

· Last modified: 2021/01/04 17:09 by

zioskenz

How do you write a binary file?

To write to a binary file Use the WriteAllBytes method, supplying the file path and name and the bytes to be written. This example appends the data array CustomerData to the file named CollectedData. dat .

Which option is used to open a file for writing as binary in C++?

The fopen function creates the file if it does not exist. r+b or rb+ Open a binary file for both reading and writing. The file must exist.

How do you read and write binary file and text file?

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files..
address of data to be written in the disk..
size of data to be written in the disk..
number of such type of data..
pointer to the file where you want to write..

How do you write data into a binary file in Python?

Write Bytes to File in Python Example 1: Open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.