Sign in to follow this  
  • entries
  • comments
  • views

Problems of holding game objects in files.

Sign in to follow this  
Black Knight


Today i was changing my code to remove all char arrays etc.
All my classes,for example an entity class was holding a member name of type char[64].
I was setting it with strncpy(); and writing to file with fwrite and reading with fread with no problems as the size was 64 I was able to pass it as a parameter to those functions.

Now I switch everything to c++ strings.And It became a problem to read the object names from files as the size of the string is not know.I had to first write the size of the string and then write the string using c_str and using the sizeofthe string again.This means more code to write just a object name.

I wonder if iostream makes any difference.ostream has read and write methods but they also take size as parameter so I guess i need to write the size out first too.

I guess this is something to deal with when using the string class.
Sign in to follow this  


Recommended Comments

Switching to the ifstreams/ofstreams won't affect the problem. This is a fundamental problem with text data in binary format files.

Your solution is a perfectly good one. The alternative is to store a null terminator at the end of the string in the file, and loop through a character at a time when reading back in until the null is found.

Both approaches have different pros and cons really.

Share this comment

Link to comment
Ah thanks its good to know that its a common problem :] I'll stick with writing the size until It causes some problem.I may even write a small function which takes a string,and a file handle as parameter and writes that string's size and itself to the file.

Share this comment

Link to comment

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