Page 1 of 1

How to use FILE correctly?

PostPosted: Sun Dec 16, 2007 2:25 pm
by asmodeus
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?

Re: How to use FILE correctly?

PostPosted: Sun Dec 16, 2007 7:00 pm
by Kalladdolf
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

Re: How to use FILE correctly?

PostPosted: Mon Dec 17, 2007 1:52 pm
by asmodeus
Yes, know knew that with save the variables. But I want know how to use the FILE.

Re: How to use FILE correctly?

PostPosted: Mon Dec 17, 2007 2:50 pm
by Kalladdolf
whee, I never used FILE...
wait for the pro's answers, I think fuzzy knows a lo about it...
happy birthday, btw

Re: How to use FILE correctly?

PostPosted: Tue Dec 18, 2007 9:13 pm
by Fuzzy
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.

Re: How to use FILE correctly?

PostPosted: Wed Dec 19, 2007 2:30 pm
by asmodeus
And what's about fwrite?

Re: How to use FILE correctly?

PostPosted: Wed Dec 19, 2007 6:28 pm
by Fuzzy
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);

Re: How to use FILE correctly?

PostPosted: Thu Dec 20, 2007 1:08 pm
by asmodeus
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.