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
Solved

Write and read an object in binary file

Posted on 2003-10-21
6
324 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
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 125 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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