Jump to content
  • Advertisement

Archived

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

johnnyBravo

possible to save things like classes,integers etc into files instead of strings?

This topic is 5401 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, im wondering if there are any functions that i could use in c++ on saving and reading files that can hold things like classes, integers, floats etc instead of just holding strings. So i can read the values straight into the right storage types etc. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
I think it''s posible but I''m no expert. Not sure how (haven''t delt with reading and printing to file). Why, for saving games?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.

Share this post


Link to post
Share on other sites
quote:
Original post by Xeneth
I think it''s posible but I''m no expert. Not sure how (haven''t delt with reading and printing to file). Why, for saving games?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.


Is this the most useless post ever or is it just me?

I personally use the standard C fopen, fread, fwrite, fclose. There it would look like:


MyClass *cdata;
int idata;

// Writing
FILE *fOut = fopen("file.dat", "wb"); // w for write, b for binary
fwrite((void *)cdata, 1, sizeof(MyClass), fOut);
fwrite((void *)&idata, 1, sizeof(int), fOut);
fclose(fOut);

// Reading
FILE *fIn = fopen("file.dat", "rb"); // r for read
fread((void *)cdata, 1, sizeof(MyClass), fIn);
fread((void *)&idata, 1, sizeof(int), fIn);
fclose(fIn);


Disclaimer: It''s no use saving pointers and loading them across separate invocations of your program. The exact binary format of a structure may depend on your compiler, so different compilations may not all be able to load the same data file.

I know there are similar methods in C++ using filestreams, << and >>. I don''t usually mess with these though.

Tom

Share this post


Link to post
Share on other sites
quote:
Original post by ParadigmShift
quote:
Original post by Xeneth
I think it''s posible but I''m no expert. Not sure how (haven''t delt with reading and printing to file). Why, for saving games?

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.


Is this the most useless post ever or is it just me?


No, not just you.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites
You could be clever about it and give each class a writeToFile() method, so the class would know how to write itself. Ultimately, it will still be strings, but at least you can abstract the tedious aspects.

Share this post


Link to post
Share on other sites
I think what SpaceRook is trying to tell you is that saving a class directly to a file is very dangerous. Any pointers would be invalid, the files wouldn''t be portable due to compiler padding of classes, and you run into a hell of a lot of trouble with templates and polymorphism.

Share this post


Link to post
Share on other sites
+1 to the previous poster.

You might also want to have a look at how the Serializable interface works in Java.

Share this post


Link to post
Share on other sites
quote:
Original post by Zahlman
+1 to the previous poster.

You might also want to have a look at how the Serializable interface works in Java.


yeah thats where i got the idea from, but i have only breifly used it,


======
so i guess i should just store things as strings then?

the thing is tho, i wouldnt be saving pointers, just classes and other data types.

i''d so rather do that, but i guess everyone is saying that is a bad idea.

But this program would probably only be used with ms vc++6 anyway...


Share this post


Link to post
Share on other sites
quote:
Original post by ParadigmShift
quote:
Original post by Xeneth
I think it''s posible but I''m no expert. Not sure how (haven''t delt with reading and printing to file). Why, for saving games?


Is this the most useless post ever or is it just me?



Is it just me or that was completely unnecessary?
He DID try to help. That sure is no reason to get flamed.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!