Binary Mode operations

File can be in binary mode or text mode. Binary mode deals with data records. These records can be in the form of a single byte or several bytes. Most often structured objects are stored in binary mode. C library has two API routines fread and fwrite. fread is to read records from the file and fwrite is to write records to a file.

Binary Mode fwrite

fwrite takes four arguments. First argument is the pointer where binary record is located. Next is the size of the record. Next to this is the number of records to write. Last argument is the file pointer. It returns the number of records written.

int fwrite (void * ptr, size_t record_size, size_t num_records, FILE * fp);

Binary Mode fwrite C code

/* Student Database Add records */
#include <stdio.h>
typedef struct _student_t
{
  char name[20];
  int roll;
  int std;
10  } student_t;
11  int main(int argc, char *argv[])
12  {
13    FILE *fp;
14    student_t s;
15    memset(&s, 0, sizeof(s));
16    fp = fopen ("records.dat", "wb");
17    if(fp) {
18      printf ("== Student Database Add records ==\n");
19      printf ("Name : ");
20      scanf ("%[^\n]", s.name);
21      fflush (stdin);
22      printf ("Roll : ");
23      scanf ("%d", &s.roll);
24      printf ("Std : ");
25      scanf ("%d", &s.std);
26      if (fwrite(&s, sizeof(s), 1, fp)) {
27        printf ("Record added.");
28      }
29      fclose(fp);
30 
31    }
32    return 0;
33  }

Output

== Student Database Add records ==
Name : Student 1
Roll : 1
Std : 1
Record added.

Binary Mode fread

fread takes identical arguments but the direction is read instead of write. First argument is the pointer where binary record is located. Next is the size of the record. Next to this is the number of records to write. Last argument is the file pointer. It returns the number of records read from the file.

int fread (void * ptr, size_t record_size, size_t num_records, FILE * fp);

Binary Mode fread C code

/* Student Database Display records */
typedef struct _student_t
{
  char name[20];
  int roll;
  int std;
} student_t;
int main(int argc, char *argv[])
10  {
11      FILE *fp;
12    student_t s;
13    memset(&s, 0, sizeof(s));
14    fp = fopen ("records.dat", "rb");
15    if(fp) {
16      printf ("== Student Database Display records ==\n");
17      if (fread (&s, sizeof(s), 1, fp)) {
18        printf ("Name : %s, Roll : %d, Std : %d\n", s.name, s.roll, s.std);
19      }
20      fclose(fp);
21 
22    }
23    return 0;
24  }

Output

== Student Database Display records ==
Name : Student 1, Roll : 1, Std : 1

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

fopen, fdopen, write vs append mode, binary vs text mode, binary mode, fseek, ftell, rewind, fprintf, fscanf, fflush, poll, select,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan