Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Write and read an object in binary file

Posted on 2003-10-21
6
Medium Priority
?
330 Views
Last Modified: 2010-05-18
hi there
i wanna to write and read a complex data as a binary ..in and from a binary file
thanks
0
Comment
Question by:mhd-it
[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
6 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9592827
You can write an integer to a file by opening an output file stream, and then writing the integer to it. Something like:

#include <fstream>

ofstream fout;

int i=233;
fout.open("data");
fout<<i;
fout.close();

Reading it back in is the analogous process with an file input stream.
0
 
LVL 8

Accepted Solution

by:
Exceter earned 375 total points
ID: 9594237
You can write an object to file by saying,

out.write( (char*)&myobject, sizeof(myobject) );

where out is an instance of ofstream.

Cheers!
Exceter
0
 
LVL 7

Expert Comment

by:burcarpat
ID: 9595912

  http://www.parashift.com/c++-faq-lite/serialization.html

will give you a head start.  also check out serialization12.zip under

    http://groups.yahoo.com/group/boost/files/

-- ba
0
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!

 
LVL 16

Expert Comment

by:imladris
ID: 9616392
Did any of those responses help?

If any did, you should select the response(s) that helped you, and grade it (them).

If not, perhaps a clarifying question would help.
0
 

Author Comment

by:mhd-it
ID: 9629348
thanks for all guys ..who's help me
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9629750
You are most welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

719 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