Jump to content

  • Log In with Google      Sign In   
  • Create Account

Saveing and loading files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 ThePointingMan   Members   -  Reputation: 196

Like
0Likes
Like

Posted 24 March 2013 - 05:03 PM

Saving and loading is the one thing that just terrifies me, I'm sure it's irrational as it can't be that hard but I don't really know where to start. I've done it before where I read or write to a text file to get information and build a map out of it, but I feel like that is kind of messy... I figured ya'll might be able to give me some insight on this.

At this current moment in time I am working on a program that gives you a 2d grid that you can alter, I want to save the information of each tile and use it in a game I am also working on. Is saving it to a text file really the best answer for me or is there another way?



Sponsor:

#2 EddieV223   Members   -  Reputation: 1406

Like
5Likes
Like

Posted 24 March 2013 - 05:20 PM

You can save in text or binary files.  Binary is much faster.  If you use fstream's then you specify ios::binary in the ctor. 

 

You need to know the format of the binary file when you read it back in.  Often files will have a header part and then the data part.  The header gives information about what to expect in the data part.

 

http://www.cs.fredonia.edu/~arnavut/classes/cs221/binaryfiles.pdf

 

When writing to a binary file stream use the .write() method, when reading use the .read() method.

 

// these two lines write an int

int myInt = GetSomeInt();

ifs.write( (char*)&myInt, sizeof(myInt) );

 

// these two lines read an int

int myInt = 0;

ifs.read( (char*)&myInt, sizeof(myInt) );

 

In both cases you take your int get its address via the address of operator and cast it to char*, since a char should be 1 byte.  The next parameter is the amount of bytes to read or write, since the example uses an int we use sizeof(myInt) which on most systems should be 4.  So these two functions take a starting address and a count of how many bytes to read/write.


Edited by EddieV223, 24 March 2013 - 05:26 PM.

If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#3 AllEightUp   Moderators   -  Reputation: 4211

Like
1Likes
Like

Posted 24 March 2013 - 07:11 PM

Saving and loading is the one thing that just terrifies me, I'm sure it's irrational as it can't be that hard but I don't really know where to start. I've done it before where I read or write to a text file to get information and build a map out of it, but I feel like that is kind of messy... I figured ya'll might be able to give me some insight on this.

At this current moment in time I am working on a program that gives you a 2d grid that you can alter, I want to save the information of each tile and use it in a game I am also working on. Is saving it to a text file really the best answer for me or is there another way?

 

Loading and saving can be a really horrible thing to deal with on occasion, especially if you try to build the serialization engine at the same time as the data you are trying to load/save.  I would actually suggest you might consider doing this as a two stage process.  Use an existing framework for serialization, I'd probably suggest Boost or Sweet for this as a starting point.  In this way you can avoid most of the issues with the low level serialization portion for a while, get your work done and then deal with serialization separately.  The nice thing about the existing solutions is that they are pretty low impact and won't require a lot of thought/code to make them work so you are not doing much wasted work in order to break up your dev tasks.

 

It's just a thought on how you might simplify your life for a while.. :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS