Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-08-10
2
Medium Priority
?
189 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

609 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