Link to home
Start Free TrialLog in
Avatar of tonitop
tonitop

asked on

Writin/Reading struct from/into binary file? (DOS)

How do I read and write struct into file? Here's simple struct:

struct names
{
char name[20];
int age;
};

struct names list[20];

I know I can use fputs() and write the whole struct into ascii file in one for-loop, but now I would like to know how I can use binary files. Is there similar ways to do it in a loop? Example would be nice.

I'm using Turbo C++ 3.0 and DOS (yes, it's still alive :)
ASKER CERTIFIED SOLUTION
Avatar of bjornh
bjornh
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... and, of course, use fread() to get it back.

You have to be careful with the file position though.  fseek() and ftell() are your friends.