Jump to content
  • Advertisement


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


Problems with File Streams

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

Hey, I''m working on some simple high score table code using file streams and I am running into some problems. I save all of the data into a binary file. I use a struct that contains a string and int as each record of the high score table. When I first run the program, the high score table works fine. The problem occurs when I try to run the program anytime after the first. The program will simply crash every time I try to do anything with the high score table, but it will work again if I delete the binary file storing the high score table (creating a new blank high score table). Anyway, I hope this all makes sense. Does anyone know by any chance what can cause this? Thanks!

Share this post

Link to post
Share on other sites
A bit more info would help. How are you reading/writing data from the struct? What are you using for strings? std::string? char* with dynamic allocation? a fixed sized char array?

Share this post

Link to post
Share on other sites
Code would be helpful.
My guess is that the program is failing to open the stream the second time, and so no data is being read, so you''ll be doing stuff with NULL pointers probably.

HTH, Steve

DirectX Programmer
Soon to be the new Bill Gates

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
As Oxyacetylene mentioned, it is difficult to pin down the problem without more info. However, there are two common pitfalls in this area. One or both of them may apply to you.

First, when using binary filestreams, the stream operators ''<<''/''>>'' must be avoided. Instead, one must use the ''read()'' or ''write()'' member functions.

Second, strings are usually a variable-size data type. This means that their contents are allocated on the heap. Which in turn means that something along the lines of ''write(&myString, sizeof(myString))'' will generally not suffice.

Rather, one must first emit a number representing the number of bytes in the string, then the string. Or one must write a NULL at the end to signify the end of the string.

You are likely in one of the three following cases

class foo
int myInt;
string myString;
void load(ifstream& file);
void save(ofstream& file);

void foo::load(ifstream& file)
int sizeOfString;
char* stringBuffer;
file.read(reinterpret_cast<char*>(&myInt), sizeof(myInt));
file.read(reinterpret_cast<char*>(&sizeOfString), sizeof(sizeOfString));
stringBuffer = new char[sizeOfString+1];
file.read(stringBuffer, sizeOfString);
stringBuffer[sizeOfString] = ''\0'';
myString = stringBuffer;
delete [] stringBuffer;

void foo::save(ofstream& file)
int sizeOfString = myString.size();
file.write(reinterpret_cast<char*>(&myInt), sizeof(myInt));
file.write(reinterpret_cast<char*>(&sizeOfString), sizeof(sizeOfString));
file.write(myString.c_str(), sizeOfString);

If you happen to be using a char* or a char[num], then these need a bit of modification. But you should get the idea.


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!