Jump to content
  • Advertisement


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


Outputting Data from containers to streams

This topic is 5433 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

OK, below is a bit of my code

	//Write the Number of elements of v1 out

	//Write the number of elements of v2 out

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
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
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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!