Jump to content
  • Advertisement
Sign in to follow this  
Ignifex

C++ Saving class with char* to a file

This topic is 5040 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, I'm trying to make a simple save/load system using fstream. I basically want to save a class to a file and then be able to load it again later. All the data I want to save, is all stored in one class. I decided to start simple and ran into a problem there. I am normally able to save and load int, double and char types, but I can't seem to find a way to save a char*/string (or vector, but I think it's the same problem here). With a char* the size changes as you give it a value (right?). For some reason when I do like: sizeof(my-char*-var); I always get 4. But it can't be 4, right? So if the size is not known, I can't save it. I've been searching all over, but I can't find out how to do this. How do I get the size of a char*? Or am I doing something very wrong by even thinking of the size of char*? It is a pointer after all. But then how can I save a class with a string to a file? Help is very welcome. Thanks in advance. Ignifex

Share this post


Link to post
Share on other sites
Advertisement
Yes, you are incorrect in using
sizeof(char *)
What you're going to have to do is use
sizeof(char)*(strlen(mystring)+1)
The '+1' is to include the null-terminator.

Hope that helps.

Also, it's been a while since I've done this, someone please correct me if I've made a mistake.

Share this post


Link to post
Share on other sites
Well, that does return the size in characters yes. :)
But will it help me save it to a file? I actually need the bytesize it uses.
Now this is probably because my post was focused on the size of the char* too much, but what I was actually wondering was:
How do I get the bytesize of my whole object? I think I am now beginning to understand something about the char*'s size. It's always 4, because it's a pointer and that stays the same. But then how do I make it so that everything is saved along in the file, so also the data behind the char*?
Just to make it a lot planer:
How do I save my entire class to a file, so I can load it up entirely later?
Thanks :)

Share this post


Link to post
Share on other sites
You need to make your load and save functions save each variable individually (Actually, if you have 3 ints after one another, you can write all 3 as a chunk of 3*sizeof(int) bytes).
If you have this structure:
struct foo {int n, char* str}; and str is the string "Hello World" (11 chars + NULL terminator), then you need to write sizeof(int) bytes from &n, and then you need to write strlen(str) bytes from str. You can add 1 to that if you also want to write the NULL, but its not really needed.

Theres no way to just write everything from a class in one go, unless you want to write the value of the pointers, and not the data at them.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Actually strlen(mystring) would give the total byte amount, since 1 char equals 1 byte?

Share this post


Link to post
Share on other sites
Ok, thanks.
I'll just have to look into the reading part some more then and save all variables individually (really can't wait :/) It's quite a bit of data, since I was planning on putting the actual gamedata in seperate files too, to make the gamebuilding a lot faster.
And yes, you're right. 1 char is 1 byte (forgot for a second... *cough*)

Share this post


Link to post
Share on other sites
My post does return the byte amount -- it's the size of a character times the total number of characters. This is the number of bytes you need to save.

Share this post


Link to post
Share on other sites
You had just replied while I was typing my reply, so I was actually only replying to pinguindude there, sorry :)
Yes, your post does return the byte amount, but since sizeof(char) = 1, you could just as well leave that away.

Share this post


Link to post
Share on other sites
Not a problem.

As for sizeof(char)==1, this is true, but to be honest I'm used to writing portable software for different systems, and so it's generally a good idea to put the sizeof in there just to be safe for different compilers and operating systems.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!