Jump to content
  • Advertisement
Sign in to follow this  
frogtag

Saving Variables C++/Dev-Net

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

I know how to print data to a text file as a form of saving, but this isn't very practical for saving varibles. Does anyone know how to save, specifically for now, an array?

Share this post


Link to post
Share on other sites
Advertisement
There are a few ways, one is serialize your object then save it to a flatfile, it's probably the easiest way. The most effecient way would be to output it to a Database, create a table for your array. Not only is it effecient but proper database management allows you to do some pretty cool things with your data such as sort it, update it, cross reference it ect. Check out SQL, MS access, oracle ect for database types.

Share this post


Link to post
Share on other sites
I'm probably going to get slammed here for suggesting C-style stuff but...


int a[10]={ 1,2,3,4 etc };

FILE *fp=fopen("file","wb");
fwrite(a,sizeof(int),10,fp); // to write

FILE *fp=fopen("file","rb");
fread(a,sizeof(int),10,fp); // to read



is probably the easiest way to read/write an array.

Share this post


Link to post
Share on other sites
If you want to know the c++ way then this site here is pretty good:

http://www.cplusplus.com/doc/tutorial/files.html

It covers reading binary toward the end of the article. And for quick reference the code to read and write in c++ is:
char memblock[20];

// reading in
fstream file = ("example.dat", ios::in | ios::binary);
file.read(memblock, 20);

// writing out
fstream file = ("example.dat", ios::out | ios::binary);
file.write(memblock, 20);


Just to show that the C way isn't really any simpler.

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!