How can we read/write structures from/to data files?

How can we read/write structures from/to data files?

There 2 function given in C++. They are fread() & fwrite(). Using them we can read & write structures
from/to data files respectively.

Basic representation:
fread(&structure, sizeof structure,1,fp);

 fread function recieves a pointer to structure and reads the image(memory) of the
structure as a stream of bytes. Sizeof gives the bytes which was occupied by structure.

fwrite(&structure, sizeof structure,1,fp);

 fwrite function recieves a pointer to structure and writes the image(memory) of the
structure as a stream of bytes. Sizeof gives the bytes which was occupied by structure.


By:Vivek Kr. Agarwal
Date:2031-12-08 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Subjective Links

C Subjective interview questions and answers for experienced and fresher