I would like to know how to save certain variables to a file. Well if not certain all variables. Please just tell me were a tut is on how to do this. Thanks!

08-28-2004

Vicious

Well really, you would just use the c++ standad i/o stream

Code:

#include <fstream>

int var1 = 20;int var2 = 30;

int main()
{

std::ofstream pFile("filename.txt");

pFile << var1;
pFile << std::endl;
pFile << var2;

pFile.close();

return 0;

}

I believe thats correct, I have the worst memory in the world and I usually forget simple things here and there.

08-28-2004

Rune Hunter

Alright it looks good. But what is the pFile << std::endl; for?

And do I have to save it as a .txt format?

08-28-2004

Vicious

If im not mistaken I THINK you have to do that to insert a new line into the file

so it will be

20
30

and not

2030

Im not sure, i'll look into it.
I havent done that kind of file i/o in a long while

And as far as filetype, you can change that sucker to anything you want :D

08-28-2004

sean

You can use whatever format you want, but using this method it just has to be an ASCII file, not binary. It's just standard file i/o. pFile << std:end; sends the endl character to the stream represented by pFile.

08-28-2004

Rune Hunter

Alright everyone here is so helpful!

And I get answers just minuts after I post! WOW!

08-28-2004

Hunter2

That would be space inefficient, and you could (and probably would) lose information that way if you're using doubles. Also, you wouldn't be able to write the contents of an object of a class. Open it in binary mode, and use write() and read().

This way, the file's size is 20 bytes (3 ints, 1 double). When you want to load the data then, declare 2 ints and a TestClass, and use file.read() in the same order as you write()'ed the variables. Notice the bolded items: it's ofstream, for "output file stream", just 'file' because p stands for pointer, and I changed it to .dat because you're writing binary data not text, and I added the flag std::ios::binary for writing binary data (that's what the write() does, just outputs the binary data stored in the variable instead of converting the data into a string and writing that).

Hope this helps! :)

08-28-2004

Rune Hunter

Alright I think I get yours a little less but it makes more sence. And you told me how to read the file so, lol, I'll use that way.

But thanks everyone else.

08-28-2004

Hunter2

Oh boy, beaten by like 4 people lol... Anyhoo, here goes:

Quote:

You can use whatever format you want, but using this method it just has to be an ASCII file, not binary.

'format' is the wrong word. Format refers to how you lay out the data within the file, and what goes where. I believe what you mean is 'extension', and that absolutely does not matter (even my change to .dat isn't technically necessary) as far as file i/o goes. It only matters when somebody looks at the file, and they're wondering what it's for... if they see .dat, they know they probably shouldn't mess with it; if they see .dll, they know it holds functions for some program (or several programs); if they see .doc, they know Microsoft Word or WordPad created it.

Quote:

it just has to be an ASCII file, not binary

No it doesn't, you can have a binary file with .txt extension (as I explained above). If you want proof, go to my website (link in the signature), download asv.zip from the DigiBible page, unzip it and open it in notepad or a hex editor... it is most assuredly binary.

08-28-2004

Vicious

I figured I would let Rune absorb the basic idea of writing a few variables to a file before I went into this... jeez hunter... ruin it for me will ya... and you code isnt even color coded.. jeez... punk

:p

lol, j/k, ofcourse.

Yes hunter's method is a MUCH better way of writing to a file.
Stuff like File I/O is when you really need a good book.

I personally like the explanations in C++ Black Book

08-28-2004

Hunter2

Sorry, didn't mean to steal your fire ;)

But anyhow, yes if you want to just write a few variables out in some way that will let any hojo jojo mofo look at the file in notepad and understand what you wrote, use Vicious' way... you can use ofstreams pretty much in the same way as you would use cout, it's just that when you're saving a game it's not always the best way of doing it unless you want the user to be able to modify the file in notepad. If you want to write pure data though (the actual variables, without converting them to strings), use my method.

>>I get yours a little less
Yes, you'll need to look at the actual function's parameters. I think write() and read() both take (char*, int); the char* is just a pointer to any location in memory - whether it's a structure, an array, a string, or just an int. The int parameter is the number of bytes to read/write, starting from the location in memory pointed to by the char*. So if you're writing a variable to the file, the number of bytes you're writing is the size of the variable, starting from the address pointed to by the char*. Thus,

file.write(&theTestClassObject, sizeof(theTestClassObject));

But the function takes a char*, so you need to convert the theTestClassObject* to char*:

file.write((char*)&theTestClassObject, sizeof(theTestClassObject));

I hope this clears it up a little bit, although I did use some big words... if you don't understand anything, just say so and I'll try to explain better.

**EDIT**
Err, forgot to mention... when you're reading from the file again, you need to use an ifstream, for input file stream - not ofstream.

08-29-2004

Rune Hunter

Quote:

file.write(&theTestClassObject, sizeof(theTestClassObject));

But the function takes a char*, so you need to convert the theTestClassObject* to char*:

file.write((char*)&theTestClassObject, sizeof(theTestClassObject));

That accauly made it more confusing but but after looking at it for a couple of mins I get it all now.

Thanks everyone, again!

08-29-2004

Rune Hunter

And wow I opened the .dat file with a hex editor and you can edit pretty essily. But I don't care that much.

08-29-2004

Hunter2

>>And wow I opened the .dat file with a hex editor and you can edit pretty essily.

Yup, that's how it is with files, they're really easy to modify. That's why you see so many "trainers" or "hacked saves" for games out, although for most commercial games they try to make it a little harder by encoding things ;)