47399

writing an object to a file in cpp

Question:

I am trying to write the contents of an object into a file. This is my code

void Write_data_intoFile(Customer c1) { ofstream fout("cust_details.txt", ios::out | ios::app); fout.write(reinterpret_cast<char*> (&c1),sizeof c1); fout.close(); }

This is how i call the function

c[ Customer_count ].Write_data_intoFile(c[ Customer_count ]);

The program works fine, but the contents are not written in the file. It shows a red colour mark saying it cannot open the file. How to solve this

class Customer { char name[25]; int id; }

I created the objects, globally, by

Customer c[20];

I am trying to write it in a text format

Answer1:

OK, first you need to write a function for text I/O of Customer. Traditionally that function is called operator<<. So you need something like this

ostream& operator<<(ostream& out, const Customer& c) { return out << c.name << ' ' << c.id << '\n'; }

Maybe (it's hard to be sure) you need to declare this function as a friend of your Customer class, if you get errors about access or private then you probably need to add this line

friend ostream& operator<<(ostream& out, const Customer& c);

inside your Customer class.

Then you need to call this function from inside your other function, like this

void Write_data_intoFile(Customer c1) { ofstream fout("cust_details.txt", ios::out | ios::app); fout << c1; fout.close(); }

Then you really need to read a good book on C++, it's hard to get this stuff right otherwise. Any problems post again.

Answer2:

Instead of reinventing the wheel in a potentially dangerous way <a href="http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html" rel="nofollow">Boost.Serialization</a> can solve this for you, hiding the dirty details.

Recommend

  • Sending a variable with a button-command (python 3.3 / tkinter) [duplicate]
  • How invoke APEX method from custom button
  • How to work with AMMediaType for video filters
  • How to create a new Bundle object?
  • Using android opencv apps without downloading opencv sdk manager
  • Symfony 2 error page response
  • How to unpack 32bit integer packed in a QByteArray?
  • HTML5 video only works in IE. The other browsers shows the black screen
  • Getting media player state in windows phone 7
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Memory error in python- how to use more memory
  • How to remove a SwiftyJSON element?
  • Differences in dis-assembled C code of GCC and Borland?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Ensure fsync did its job
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • ImageMagick, replace semi-transparent white with opaque white
  • Algorithm for a smudge tool?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Why is the size of this struct 32?
  • Getting last autonumber in access
  • Problems to linebreak with an int in JLabel
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • ILMerge & Keep Assembly Name
  • Running a C# exe file
  • Updated Ionic CLI but shows previous version (Windows)
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • Hits per day in Google Big Query
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • Why do underscore prefixed variables exist?