Jump to content
  • Advertisement

Archived

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

Peddler

HighScore File IO

This topic is 6614 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 is the best method to use for a HighScore File. What I am fooling around with now is this: int Score=200; char Tim[16] = "Tim"; ofstream fout("Atoll.dat",ios::out / ios::trunc / ios::binary); int x = 0; while (x < 10) { fout.put((char)Tim); fout.put((char)Score); x++; Score-=10; } fout.flush(); fout.close(); This just makes a test file with different score values...then I use this to load it into my highscore array int x = 0; ifstream fin("Atoll.dat", ios::in / ios::binary); while (x < 10) { HiScore[x].Name = (char)fin.get(); HiScore[x].Score = (int)fin.get(); x++; } fin.close(); The score works correctly, but I know I am doing something wrong with placing the name char in the file. Does anyone have any suggestions on this dilema?? Thanks a bunch. -Tim

Share this post


Link to post
Share on other sites
Advertisement
There is a couple of things you are doing wrong. When you use put() it can only do characters. If your score is bigger than 256 i don''t think it will work. And using put on a character array will probably only write the first character in it. Actually, it might write the address of the char array. You should use the << operator. Replace those lines with :
fout << Tim;
fout << Score;
That should work.

*** Triality ***

Share this post


Link to post
Share on other sites
If I were you, here''s how I''d do it. I''m a major C++ streams fan, so here''s the load:

You can look at this one of two ways... If you want to write the score file as an ascii file (which can be edited by a text editor), then here''s the method:


/////
ofstream fout("score.txt");
fout << Tim << endl;
fout << Score << endl;
fout.close();

ifstream fin("score.txt");
//Use getline() just in case there is spaces
//in the char array
fin.getline(Tim, 80);
fin >> Score;
fin.close();
/////


That''s how you do the ascii method. Notice how you can use the >> and << to input and output. The >> used for input will cut off after any whitespace, so that''s why I used getline(). For instance, if the name you outputted to the file was "Johnny B." And you used "fin >> Tim;", then Tim would now be equal to "Johnny" instead of what you wanted, "Johnny B.".

Okay, here''s how to do binary I/O.


/////
ofstream fout("score.dat", ios::binary);
fout.write((char *)(&Tim), sizeof(Tim));
fout.write((char *)(&Score), sizeof(Score));
fout.close();

ifstream fin("score.dat", ios::binary);
fin.read((char *)(&Tim), sizeof(Tim));
fin.read((char *)(&Score), sizeof(Score));
fin.close();
/////


And there you have it! The read() and write() methods are almost identical. Actually, I guess they are identical. You must cast the variable you want read or written to the (char *) type, then tell the function how many bytes the entire structure is. You can also use structures, classes, and just about any other data type with this as well. And that''s just about all you have to know for I/O for about 75% of file operations. Good luck! E-mail me below if you have any further questions.


ColdfireV

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!