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

QuestionsCategory: C ProgrammingHow can we read/write Structures from/to data files?
Neeraj Dhyani asked 3 weeks ago
Please follow and like us:
1 Answers
AdminAdmin answered 3 weeks ago

To write out a structure we can use fwrite() as Fwrite( &e, sizeof(e),1,fp);Where e is a
Structure variable. A corresponding fread() invocation can read the structure back from file. calling fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(),however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler
dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files youre writing will ever be interchanged between machines.

Please follow and like us: