Jump to content
  • Advertisement
Sign in to follow this  
andyb716

Saving data to a file in binary?

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

Hi, I'm using c++ and the only way I could write and read in binary is the std::fstream. I've tried the FILE *file. and the std::ifstream and ostream and I couldn't get them to write in binary. What I need it for is to save scores for my game, If I use text then a player can go edit there scores and say they got a billion points on level one. What I have now is that is saves a couple scores correctly and then once I get the third score in there some of it gets erased or doesn't pull up properly. When I view the file its all on one line. Is that ok, because I try to put \n's to break the different scores. I seperate the name level and score with a blank space and then get the whole line with getline and the break apart the line with substrings. Maybe it putting the whole line in there and not finding line breaks. Can someone please tell me what file methods I should use and what ones can write binary? I've read on google they all can but I just can't get it to work, besides using fstream Thanks.

Share this post


Link to post
Share on other sites
Advertisement
well with C you would use fread() and wb or rb to make it binary.
and for C++ i think you need to use the qualifier ios::binary.

this is what i remember from a few years ago.

anyway if you [google] the underlined you should find more info

Share this post


Link to post
Share on other sites
std::ofstream fout("highscores.bin", std::ios::out | std::ios::binary);

int highScores[10], numHighScores = 10;

fout.write((char *)&numHighScores, sizeof(int));

fout.write((char *)highScores, numHighScores * sizeof(int));
...
std::ifstream fin("highscores.bin", std::ios::in | std::ios::binary);

fin.read((char *)&numHighScores, sizeof(int));

fin.read((char *)highScores, numHighScores * sizeof(int));

Share this post


Link to post
Share on other sites
Why would it show up as text? You can edit it in a text editer, but shouldn't it show up as misc. ASCII characters isntead of a string of human-readable numbers?

Share this post


Link to post
Share on other sites
Write a test example that doesn't work and show us the code. The compilable code shouldn't need to be more than about 10 lines.

Pete

Share this post


Link to post
Share on other sites

#include <fstream>
#include <iostream>

std::ofstream outFile;

int main(){

char *String = "This is some text\n This is some more text.";

outFile.open("textfile.bin", std::ios::out | std::ios::binary);

outFile.write(String, strlen(String));

outFile.close();

return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!