Solved

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

Posted on 1998-08-10
2
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

695 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