How to use FILE correctly?

Game Editor comments and discussion.

How to use FILE correctly?

Postby asmodeus » Sun Dec 16, 2007 2:25 pm

I don't know very well how to use FILE (fopen, fread, fwrite etc.).
When I use FILE I should use it like this:
Code: Select all
FILE *myfile; //like "int myvar" or "char *mychar" I think
myfile = fopen("../folder/filename", "r+b"); // opens "filename" and read that binary file
fread( ? ); // I don't know what to write
fclose(myfile); //close the file

I want this: I've got an actor variable (myactor.myvar[0]) with an array of 10. First I want to save the variables in a binary file (../folder/filename) and with an event the game should load the variables and give them the actor.
Can anybody show me a correct code with explanations for the arguments in fwrite / fread?
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: How to use FILE correctly?

Postby Kalladdolf » Sun Dec 16, 2007 7:00 pm

well, to save and load variables and stuf, you don't need the FILE functions at all!
when you create or edit a variable, there is a button that says "save group".
you click on it and write a name of a group you'd like to save it in.
then when you want to save a variable, in script editor pick the function "saveVars".
then you get to pick which group to save and in which file.
just enter a filename (for example: "variables").
then it will (when the action is executed create a file (in the same folder as the game) called "variables.dat".
all the variables in this specific group are saved there.
to load the variables, just pick loadVars and pick which group to reload.

phew, Ich glaub, das is zwar kompliziert, aba...
:D
User avatar
Kalladdolf
 
Posts: 2427
Joined: Sat Sep 08, 2007 8:22 am
Location: Germany
Score: 120 Give a positive score

Re: How to use FILE correctly?

Postby asmodeus » Mon Dec 17, 2007 1:52 pm

Yes, know knew that with save the variables. But I want know how to use the FILE.
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: How to use FILE correctly?

Postby Kalladdolf » Mon Dec 17, 2007 2:50 pm

whee, I never used FILE...
wait for the pro's answers, I think fuzzy knows a lo about it...
happy birthday, btw
User avatar
Kalladdolf
 
Posts: 2427
Joined: Sat Sep 08, 2007 8:22 am
Location: Germany
Score: 120 Give a positive score

Re: How to use FILE correctly?

Postby Fuzzy » Tue Dec 18, 2007 9:13 pm

fread can be a little clunky.

You seem to have the rest(like FILE, open and close) down though.

fread takes 4 elements.

The first is a pointer, or in easier terms, the name of the variable that you wish to read into. Lets say we want to read an actors health from file, and the variable is called Health. Its a integer.

fread(Health,....);

the next element tells the computer how big, in bytes, that variable is. An integer is 32 bits otherwise known as 4 bytes. 4 is the number we are looking for, but we dont want to use it directly. At one time an integer was only 2 bytes. Already on 64 bit machines an integer is 8 bytes long. So in the interest of having your game work on all machines, its best to calculate this in code. There are are also some structures that vary in size.

To calculate this we use a function called sizeof(). sizeof(int) will return 4 on a 32 bit machine, and 8 on a 64 bit machine.

Here is the fread so far...

fread(Health, sizeof(int),....);

note that you dont fill in the variable name. It would work in this example, but possibly not with a struct.

the third element is how many times to write it. Unless you are writing from a struct, just put one.

fread(Health, sizeof(int), 1,...)

and the last element is simply the pointer that you created with FILE.
Code: Select all
fread(Health, sizeof(int), 1, myfile);


If you need clarification, please ask, otherwise, I hope that helps.
Last edited by Fuzzy on Wed Dec 19, 2007 6:29 pm, edited 1 time in total.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: How to use FILE correctly?

Postby asmodeus » Wed Dec 19, 2007 2:30 pm

And what's about fwrite?
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: How to use FILE correctly?

Postby Fuzzy » Wed Dec 19, 2007 6:28 pm

Code: Select all
    fread(Health, sizeof(int), 1, myfile);


Sorry. Note that its int and not integer.

fwrite is the same general format.
Code: Select all
    fwrite(Health, sizeof(int), 1, myfile);
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: How to use FILE correctly?

Postby asmodeus » Thu Dec 20, 2007 1:08 pm

OK. I haven't understand it 100% but maybe it helps if you tell me, how to save an integer variable with array ( [10][5] ) in a file.
The name of the variable is myvar and the file name is myfile.
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron