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.
DaVince This fool just HAD to have a custom rating
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.
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.
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.
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)
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
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.
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;
@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
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.
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.
Edited by the Author.
hOY! Pinoy Ako..
DaVince This fool just HAD to have a custom rating
Registered 04/09/2004
Points 7998
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.