The Daily Click ::. Forums ::. Non-Klik Coding Help ::. Saving/loading stuff in C++?
 

Post Reply  Post Oekaki 
 

Posted By Message

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
7th November, 2005 at 00:04:32 -

How do you create like a file in C++ and save/load it? Let's say I have a character (as in a NPC in a RPG). How do I save that character to disk and load him/her when I want to? The character could be like an array, structure, class, or anything else that stores data. Just want to make a damn saveable character...

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

DaVince

This fool just HAD to have a custom rating

Registered
  04/09/2004
Points
  7998

Game of the Week WinnerClickzine StaffHas Donated, Thank You!Cardboard BoxDos Rules!
7th November, 2005 at 12:13:42 -

http://www.cprogramming.com/tutorial/lesson10.html
http://www.cprogramming.com/tutorial/cfileio.html

Good enough?

Image Edited by the Author.

 
Old member (~2004-2007).

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
14th November, 2005 at 23:27:59 -

ifstream a_file ( "filename" )?

Dunno, don't really help much. I mean something on C++, not C as C is a bit slow, which counters the reason why I'd make it in C++ instead of MMF. Anything on saving files, loading files, comands to input more than one thing into a file, etc?

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

Radix

hot for teacher

Registered
  01/10/2003
Points
  3139

Has Donated, Thank You!VIP MemberGOTW WINNER CUP 1!GOTW WINNER CUP 2!GOTW WINNER CUP 3!GOTW WINNER CUP 4!
15th November, 2005 at 01:00:33 -

ofstream outStream("[filename]", ios::[how you want it to behave]);
outStream << [stream to save];
outStream.close();

And then the same thing with istreams, ifstream inStream("[file]") and getline(inStream, [string variable]). That's pretty much how you do it. There's plenty you can do with iostream. No doubt there are classes to streamline it for games, but you'll have to do some hunting.

 
n/a

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
15th November, 2005 at 01:30:17 -

Hmmm... interesting. I'll try that out later. Thanks!

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
3rd December, 2005 at 08:27:34 -

I mean something on C++, not C as C is a bit slow

Not sure who told you that but I think you'll find that the C method is faster in 9/10 cases. At least 4 and usually over 7/8 times faster for file I/O!

(fopen, fputs, fwrite, fclose)

Image Edited by the Author.

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
4th December, 2005 at 11:45:16 -

C is actually marginally faster than C++ as a language, because things like classes incur a very small overhead. This overhead is negligable though, it's like the odd instruction here or there out of your 3 billion per second.

 
- Tigerworks

vortex2



Registered
  27/05/2002
Points
  1406
4th December, 2005 at 19:18:08 -

Ya know, I am quite partial to using xml to save data soo I recommend using the tinyXML class : http://www.grinninglizard.com/tinyxml/

You could do like:
<Characters>
<Character Name="Vortex">
<Stats Health=100 Dex=10 Strength=20/>
<Level ID=1>
<Door ID=5 Locked=0/>
</Level>
<Items>
<Item Name="Sword" Damage=50/>
</Items>
</Character>
</Characters>

Or umm something like that ^^.

 
A bit of insanity with every bite!

Leerz



Registered
  31/08/2004
Points
  995
22nd December, 2005 at 15:07:08 -

sir MUZ, did you manage to get things working?
i'm having a hard time getting over this thing too.
here is my code (write to file)
#include<stdio.h>
main()
{
char z[10]="";
FILE *fp;

fp=fopen("c:\\huh.txt", "w");
gets(z);
/*fprintf(fp, "Testing...\n");*/
fprintf(fp,z);
/*printf("\nDone!");*/

}

though after that, i cant seem to figure out how to retrieve my data.
Thanx anyways.

 
hOY! Pinoy Ako..

DaVince

This fool just HAD to have a custom rating

Registered
  04/09/2004
Points
  7998

Game of the Week WinnerClickzine StaffHas Donated, Thank You!Cardboard BoxDos Rules!
23rd December, 2005 at 16:24:18 -

Last post date: 12/4/2005.
Yours: 12/22/2005.



 
Old member (~2004-2007).

Leerz



Registered
  31/08/2004
Points
  995
26th December, 2005 at 10:33:44 -

@DaVince [Black Wizards] - i don't see anything wrong with that.
the topic is not even a month old, nothing odd with it.
it aint even digging for old topics to spam.

-Leerz

 
hOY! Pinoy Ako..

DaVince

This fool just HAD to have a custom rating

Registered
  04/09/2004
Points
  7998

Game of the Week WinnerClickzine StaffHas Donated, Thank You!Cardboard BoxDos Rules!
26th December, 2005 at 11:00:01 -

The question was already answered, or he might have figured it out himself in this almost-a-month. So yeah, it wasn't really necessary to asnwer anymore.

 
Old member (~2004-2007).

Leerz



Registered
  31/08/2004
Points
  995
27th December, 2005 at 13:11:55 -

that is why the post is addressed to mr. Muz, so he can say if it was solved or whatever. if the solution worked or something.. coz ya see. i'm havin a hard time gettin it to work (makin sure that i know what i was doing)
-i'm also having a hard time dealing with the write to file thing.


Image Edited by the Author.

 
hOY! Pinoy Ako..

DaVince

This fool just HAD to have a custom rating

Registered
  04/09/2004
Points
  7998

Game of the Week WinnerClickzine StaffHas Donated, Thank You!Cardboard BoxDos Rules!
29th December, 2005 at 12:55:57 -

Well, nevermind. I was annoyed at the time.

I haven't really experimented much with C(++) file storing/loading yet, but I think the correct function to read a file was readf()... Correct me if I'm wrong.

EDIT: I loked it up, it's readf() alright... Go look how to use it yourself.

Image Edited by the Author.

 
Old member (~2004-2007).
   

Post Reply



 



Advertisement

Worth A Click