Computing.Net > Forums > Programming > Can't write object to file c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Can't write object to file c++

Reply to Message Icon

Name: dave19
Date: December 5, 2008 at 05:33:00 Pacific
OS: Windows XP SP2
CPU/Ram: Intel Duo Core 512
Product: Hewlett-packard / NX6310
Comment:

I'm trying to write an object to file "List.dat" using multiple I/O class object of fstream. It works when i open the file to write and read from it. But it doesn't seem to work when i open the file to read first to check the written objects before writing a new object to it.



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: December 5, 2008 at 08:44:22 Pacific
Reply:

What do you pass as the second argument (the openmode argument) to the fstream constructor?

(By the way, the fstream constructor is described on page 666 of the C++ Standard, so it must be a bit of a devil ;-)


0

Response Number 2
Name: dave19
Date: December 5, 2008 at 14:01:34 Pacific
Reply:

ios::in in open() for reading and for second time ios::out in open() for writing in file.


0

Response Number 3
Name: klint
Date: December 8, 2008 at 06:40:11 Pacific
Reply:

When you open it the second time (i.e. for writing), you need to make sure you close it first. You can't open it if it is already open. Also, you should check the status of the file after opening (and before every read or write operation.) But you left me wondering why you've taken this approach. Normally, if you want to read and write the same file, you would open it for both read and write (passing ios::in | ios::out) or else you might as well create two objects, and ifstream and an ofstream.


0

Response Number 4
Name: dave19
Date: December 8, 2008 at 12:19:36 Pacific
Reply:

Thanks, I suppose It was missing the close() at the end, but then i thought to use two different objects to open it anytime for read or write.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Batch file to check a dir... Batch Script Command Help



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Can't write object to file c++

writing data to file in java www.computing.net/answers/programming/writing-data-to-file-in-java/14129.html

Write to file noob question www.computing.net/answers/programming/write-to-file-noob-question/13381.html

Can't put quotes around files names www.computing.net/answers/programming/cant-put-quotes-around-files-names/14321.html