Page 1 of 1

text file save and load?

PostPosted: Thu Sep 29, 2011 10:50 am
by CoFFiN6
Hello everyone :D

I need your help again.
In my game I want you to write words with a input.And I want to take that input and save it exactly like it is in a windows .txt file in my game directory.And then when I open the game and press load it would load that text again and place it on screen.

How would I do this? I have searched for other posts,I have found sprintf,loadvars,savevars and even cout and cin which I don't thinks gona work.

So basicly how to export to .txt and then how to inport it again. Also the .txt file should be created and not be edited on a exsisting one.
Hope I'm making sense here but any pointers would be much pleasing! :D't thinks gona work.

So basicly how to export to .txt and then how to inport it again. Also the .txt file should be created and not be edited on a exsisting one.
Hope I'm making sense here but any pointers would be much pleasing! :D

Re: text file save and load?

PostPosted: Thu Sep 29, 2011 10:54 am
by CoFFiN6
Ignore the repeated paragraph^

Re: text file save and load?

PostPosted: Fri Sep 30, 2011 12:23 am
by skydereign
It depends on the formatting, but what you need are FILE*s and fprintf, fscanf/fgets. All of these handle FILE* which are how C deals with file io. To initialize a file, you need to declare a FILE* and use fopen to open the file.
Code: Select all
FILE* file = fopen("file_name.txt", "w");

The "w" is what you call the format. It sets the permissions for the FILE*. So, w will create the file (erase its content if it has any), and allow you to write in it. Certain read features won't work though. "r" would allow you to read through the file, so using fscanf or fgets, but you can't write to the file. If you use "r+" you can read and write to the file. There are others, and a quick search will tell you what they all do.

Now when you have your file initialized, you can now use fprintf, and the other file functions.
view -> Create Actor -> Script Editor
Code: Select all
FILE* file = fopen("file_name.txt", "w");
fprintf(file, "test print: %s", name);
fclose(file);

This will print into the file,
Code: Select all
test print: view

One thing to note is you should always close the file when you are done with it. fprintf and fscanf work pretty much identically to sprintf and sscanf. So if you want to open the file, to read in its text, you can use fscanf or fgets to read the information in. And when you need to save, you print it out with fprintf.

Re: text file save and load?

PostPosted: Thu Dec 15, 2011 6:15 am
by CoFFiN6
Thanx Skydereign!!

I just tested this and it works perfect. I made an actor and put it as input. Then on that actor Add/Key Down/Script Editer(key RETURN) and copied your code there. I only changed the 3rd line to

fprintf ( file , inputActor.text , name );

So when I run this, I write something and press enter and it writes that input in the txt file.

And I did search the other FILE* writing and read stuf. For those who also need it here:

r - open for reading
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)

Thanx again :D

Re: text file save and load?

PostPosted: Thu Dec 15, 2011 3:26 pm
by Game A Gogo
there is also the r+b and w+b, which are for reading the files as binary, but I don't know what difference it makes

Re: text file save and load?

PostPosted: Thu Dec 15, 2011 8:28 pm
by skydereign
You can insert b anywhere after the first character so, "rb", "r+b", "rb+", and same for the write and append. The reason for declaring it a binary file is for portability (an issue we don't need to consider). You can also use "t" to specify text files, but since there is no difference between opening a file in binary or text, it has no effect. I think it's really just so the person reading the code knows what type of data is in it.