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 :)
tonitopAsked:
Who is Participating?
 
bjornhConnect With a Mentor Commented:
Use fwrite to store a struct to a file:

f = fopen("file", "wb");

fwrite(list, sizeof(stuct names), 20, f);

I think this should work.

Good luck, Bjorn
0
 
alexoCommented:
... 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.
0
All Courses

From novice to tech pro — start learning today.