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.