Solved

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

Posted on 1998-08-10
2
180 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
Comment Utility
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
Comment Utility
... 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now