This message was edited by stober at 2006-7-23 11:50:40
: I have a question about fstream
: I am making a japanese language tutor program... (like math blaster)
: and i am keeping it simple
:
: struct KKR{ int x,y,w,h,bmp; }; // Kana/Kanji/Romanji KKR
:
: struct LEVEL_DATA{
: KKR kana[50];
: KKR kanji[50];
: KKR romanji[50];
: string ON[50];
: string KUN[50];
: string ENG[50];
: char LEVEL_BMP[256];
: }level;
:
:
: I will be making files for each level ie: level_1.jbd
:
: KKR will hold all the information for coordinats on the bitmap
: with all the Kanji, Kana, Romanji text... becase i will later have them flying across the screen shooting at the right answer.. yada yada
: this way file sizes are short.. loading the levels may take longer but not much.
:
: the [50] after everything in the LEVEL_DATA represents EACH diffrent word
:
: ie: if the user was learning KANJI and needed to select the right KANA
: the program would see if(KANA[4] == KANJI[4]){ /* Correct answer */ }
: if(KANA[4] == KANJI[2]){ /* Incorrect asnwer */ )
:
: string is used becase char* was crashing at run-time while using
: cin >> str;
:
: this is where/when the user would need to enter the meaning of a KANJI
: ie: the KANJI for 'water' was shown.. user would type in 'mizu' and mizu would be compared to KUN[?]
:
: and LEVEL_BMP is the filename of the bitmap being used...
:
: ok now you understand the struct
: if it is the reason let me know
:
: this is where the program fails:
:
:
: file.open(level_file, ios::trunc | ios::out);
: file << rec_tot << " " << rec_num << " ";
: file.write((char*)&level,sizeof(LEVEL_DATA));
: file.close();
: // File is created....
:
: file.open(level_file, ios::in);
: file >> rec_tot;
: file >> rec_num;
: file.read((char*)&level,sizeof(LEVEL_DATA));
: file.close();
: // File is loaded....
:
:
: When i check my data i entered it is messed up:
:
: i enter 1's 2's 3's etc for all the x,y,h,w,bmp of KKR's and a quick one word for the ON KUN ENG and then i view it on the screen...
: it all looks good.. so i save it with the code above..
: still looks good..
: then i close program..
: re-open program..
: load same file..
: ok so far so good..
: view contents with same viewer.. and crash
:
: i can't figure it out.. my code should be good... but... i cant figure out the DEV_C++ debuger...
:
: if you want copy of my full code and my program that is crashing let me know i'll email
: thank you
:
: keeperols@comcast.net
:
The file is opened in text mode, so if you are doing this on MS-Windows there may be a problem with the os interpreting some of the binary data in that structure. If you want to leave the file in text mode then read/write each of the structure fields individually and ignore the remainder of my comments below.
If you change the file type to binary mode then that structure cannot be written and read in one big swoop like you are attempting to do because all that is getting writting is the std::string class itself and not the actual text of the class object. And that is similar problem when you used char* instead of std::string. You can resolve this problem in a couple ways:
1) replace std::string with fixed-length char arrays. Here I just plugged in 255 as the length of the strings, but you can adjust that to whatever you think appropriate for your program. Then the write/read should work.
: struct LEVEL_DATA{
: KKR kana[50];
: KKR kanji[50];
: KKR romanji[50];
char ON[50][255];
char KUN[50][255];
char ENG[50][255];
: char LEVEL_BMP[256];
: }level;
2) If you want to retain std::string in the structure you will have to make the file read/writes more complex. After writing the entire structure as you are doing now, you will have to write each of the individual strings.
file.write((char*)&level,sizeof(LEVEL_DATA));
// now write out the strings
for(int i = 0; i < 50; i++)
{
file << level.ON[i] << "\n" << level.KUN[i] << "\n" << level.ENG[i] << "\n";
}
// reading do the same thing
file.read((char*)&level,sizeof(LEVEL_DATA));
// now read the strings. Here use getline just in case the
// strings contains spaces.
for(int i = 0; i < 50; i++)
{
getline(file,level.ON[i]);
getline(file,level.KUN[i]);
getline(file,level.ENG[i]);
}