text file save and load?

Game Editor comments and discussion.

text file save and load?

Postby CoFFiN6 » Thu Sep 29, 2011 10:50 am

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
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: text file save and load?

Postby CoFFiN6 » Thu Sep 29, 2011 10:54 am

Ignore the repeated paragraph^
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: text file save and load?

Postby skydereign » Fri Sep 30, 2011 12:23 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: text file save and load?

Postby CoFFiN6 » Thu Dec 15, 2011 6:15 am

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
Don't run faster then the dragon, run faster than your friends :P
User avatar
CoFFiN6
 
Posts: 49
Joined: Sun Sep 11, 2011 8:17 pm
Location: South Africa
Score: 4 Give a positive score

Re: text file save and load?

Postby Game A Gogo » Thu Dec 15, 2011 3:26 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: text file save and load?

Postby skydereign » Thu Dec 15, 2011 8:28 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest