Archived

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

Bagpuss

Outputting Data from containers to streams

Recommended Posts

Bagpuss    122
OK, below is a bit of my code
try	
{	
	fs->open(fn.c_str(),std::ios::out|std::ios::binary);

	fs->setf(std::ios_base::dec);
	//Write the Number of elements of v1 out

	*fs<<v1.size;
	//Write the number of elements of v2 out

	*fs<<v2.size; 
Now initially each of the 2 vectors is 4 elements in size So when I run the code above it writes 44 out to my file. This is displayed in my binary file as 44, so I guess it''s not really being stored as binary. This means that when I read it back in, on my program starting, I have v1 potentially conatining 44 elements and v2 containing garbage. What am I doing wrong, I want to store 2 integers, 1 containing the size of each vector. (I know that the size of V is not necessarily an integer, but thats another problem) TIA, Bp.

Share this post


Link to post
Share on other sites
Fruny    1658
std::vector::size is a function not a variable.
Formatted IO outputs ASCII text, you need to separate your two numbers.

I assume fs is an std::ofstream*.



[ 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
Bagpuss    122
OK, thanks.(it was outputting the correct size though, so go figure.)

"I assume fs is an std::ofstream*."
Yes it is.

"Formatted IO outputs ASCII text, you need to separate your two numbers."
I opened my file in binary, so I assumed that all my data would be written out as binary, not as ASCII.

I removed my formatting code (std::ios::dec), but still the 2 numbers are written seperately, and then read back as one, so I am obviously being stupid.

The only answer I can see is to output my code in hex, but surely there has to be an easier way of storing my data in binary ?

Share this post


Link to post
Share on other sites
C-Junkie    1099
opening a file in binary means ABSOLUTELY NOTHING. repeat that a few times.

(now just note that on windows, and only on windows, opening in text mode causes \n to be turned into \r\n when writing and vice versa for reading. binary jsut means this doesn''t happen. and thats only on windows. on most other OS''s it means ABSOLUTELY NOTHING.)

so, okay, now we know opening in binary mode means NOTHING. How do we write to a file like we want to?

ofstream fp("somefile.dat");
int someint = 123;

fp.write((const char*)&someint,sizeof(someint));

and read it after that?

ifstream fp("somefile.dat");
int someint;

fp.read((cons char*)&someint,sizeof(someint));

now, ofc ourse, you have to deal with byte-size and endian issues, but i suspect you''re not writing something that will ever be used off of a 32-bit windows system, eh?

Share this post


Link to post
Share on other sites
Bagpuss    122
OK, so I can only use << and >> when I am writing formatted ASCII (ie text ?)

so there is no point in me operator overloading my streams for custom classes if I want to store my data in raw format, not as text ?

Share this post


Link to post
Share on other sites