Saving Variables C++/Dev-Net

Started by
2 comments, last by nobodynews 18 years ago
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?
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.
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 writeFILE *fp=fopen("file","rb");fread(a,sizeof(int),10,fp); // to read


is probably the easiest way to read/write an array.
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 infstream file = ("example.dat", ios::in | ios::binary);file.read(memblock, 20);// writing outfstream file = ("example.dat", ios::out | ios::binary);file.write(memblock, 20);


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

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement