• Advertisement

Archived

This topic is now archived and is closed to further replies.

Saving binayies with fstreams

This topic is 5247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve a problem with filestreams. I want to save data with file streams, but if the file already exists Windows prints the Message, this Porgram has caused a problem. I don''t know whats wrong with it. #include <fstream.h> #include <iostream.h> #define UINT unsigned int void main () { fstream file; UINT myInt = 3; // Exists the File? file.open( "Test.t", ios::in | ios::nocreate); if(file.is_open()) cout << "Offen" << endl; else cout << "Nicht Gefunden" << endl; file.close(); // Save Data file.open("Test.t", ios::out | ios::binary); file.write((char*) myInt, sizeof(UINT)); file.close(); } Thank you for your help!

Share this post


Link to post
Share on other sites
Advertisement
if i read correctly the std::ios_base::binary works only to read a stream as a binary, not to write... but i can be wrong...


ios_base::openmode
:: binary, to read a file as a binary stream, rather than as a text stream.

correct me if i''m wrong :\

Share this post


Link to post
Share on other sites
You are wrong. This is necessary, but not sufficient.

binary mode vs. text mode controls line-ending translation : on MS platforms, when you write '\n' to a file in text mode, it really writes '\r\n', on unix it's just '\n' and on Mac it's just '\r' - the reverse is done on a file read. Binary mode disables this (required if you have real binary data).

operator<< and operator>> do formatted "text" I/O.
You need to use iostream::read() and iostream::write() - or any one related function, to do unformatted "binary" I/O.



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on October 8, 2003 6:51:39 PM]

Share this post


Link to post
Share on other sites
so the diference about binary or text is just the that binary is unformatted, right?

but for example the ofstream::write() works only with char *, how i am i suposed to write an int? i have to convert it to char or exits other way to do that?

how do i do to write a class if i can''t use the operator<< ?

thanks

Share this post


Link to post
Share on other sites
but for example the ofstream::write() works only with char *, how i am i suposed to write an int?

Take the address of the int and cast that to a char*.

how do i do to write a class if i can''t use the operator<< ?

The ''hard'' way - one member at a time (hint - write a member function to do that !). The naive bit-for-bit copying of an object is generally a bad idea, as it only works for POD-classes (the C++ term for C-style structs)




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
in c I could write an entire struct to a file at a time. can I do that in c++ with a class?
what is the difference?
the way the data is stored in memory is not the same for a struct and for a class?
I think the bytes of a struct are stored sequentially and that''s why we can write all at once... but I don''t know how this works for classes :\

Share this post


Link to post
Share on other sites
You can write a whole class at once to a file (remember, a class is the same as a struct) BUT you will get troubles if
- the class contains pointers
- the class contains virtual functions
Maybe there are other causes of troubles I didn''t think of.

Share this post


Link to post
Share on other sites
quote:
Original post by VolkerG
You can write a whole class at once to a file (remember, a class is the same as a struct) BUT you will get troubles if
- the class contains pointers
- the class contains virtual functions
Maybe there are other causes of troubles I didn''t think of.


If the class contains objects that contain pointers or virtual functions or which contain objects that ...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites

  • Advertisement