Sign in to follow this  
frogtag

Saving Variables C++/Dev-Net

Recommended Posts

frogtag    121
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
senelebe    100
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
Aardvajk    13207
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
nobodynews    3126
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this