Solved

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

Posted on 1998-08-10
2
185 Views
Last Modified: 2010-04-15
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 :)
0
Comment
Question by:tonitop
2 Comments
 

Accepted Solution

by:
bjornh earned 100 total points
ID: 1252018
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
 
LVL 11

Expert Comment

by:alexo
ID: 1252019
... 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

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question