• Advertisement

Archived

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

Fstream and writing/reading binary files

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

What''s the best way to write and read a binary file..? Suppose I want a structure like this: Bytes Times Value 4 1 versionstring 10 1 flags for later use 32 x flower names 32 x gardening tools Seems to me that making a basic class to store all this information would be troublesome. Since you can''t use dynamic arrays you would have to make flower names and gardening tools fixed ones, which is just... plain bad. So, instead I thought I''d try to use the fstream.write and .read member functions but I can''t figure out how to write/read integers etc. with them.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by MagiXSphere
What's the best way to write and read a binary file..? Suppose I want a structure like this:

Bytes Times Value
4 1 versionstring
10 1 flags for later use
32 x flower names
32 x gardening tools

Seems to me that making a basic class to store all this information would be troublesome. Since you can't use dynamic arrays you would have to make flower names and gardening tools fixed ones, which is just... plain bad.


why can't you use dynamic arrays in a struct/class? Just have your struct contain a pointer and then just allocate and deallocate w/ new & delete. Maybe have a separate variable in your struct also to indicate it's current size.

quote:

So, instead I thought I'd try to use the fstream.write and .read member functions but I can't figure out how to write/read integers etc. with them.


You can store integer variables in binary like this:


int foo_num=10;
ofstream foo_out("filenamehere", ios::out|ios::binary);//maybe ios_base

//for writing to file

foo_out.write(reinterpret_cast<char *>(&foo_num),sizeof(int));
foo_out.close();
//for reading from file

ifstream foo_in("filenamehere", ios::in|ios::binary);
foo_in.read(reinterpret_cast<char *>(&foo_num),sizeof(int));
foo_in.close();


so it should look something like that.

Edit: whoops my bad, should be read and write without the f. ok fixed it now





--{You fight like a dairy farmer!}

[edited by - Greatwolf on August 17, 2003 6:19:15 PM]

Share this post


Link to post
Share on other sites
quote:

why can''t you use dynamic arrays in a struct/class? Just have your struct contain a pointer and then just allocate and deallocate w/ new & delete. Maybe have a separate variable in your struct also to indicate it''s current size.


Because if you use dynamic arrays you can''t write/read the data contained within it directly to a binary file and so they serve no purpose so such.

quote:

//for reading from fileifstream foo_in("filenamehere", ios::in|ios::binary);foo_in.fread(reinterpret_cast(&foo_num),sizeof(int));foo_in.close();


I can''t seem to get this to work, foo_num is empty after the code has been executed

Share this post


Link to post
Share on other sites
quote:
Original post by MagiXSphere

Because if you use dynamic arrays you can''t write/read the data contained within it directly to a binary file and so they serve no purpose so such.



What about just looping through it and write each element one by one. So store number of elements first then do the loop. When you read it back in read the size in and then loop it for size times.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites

  • Advertisement