Using File
#3 Members - Reputation: 122
Posted 01 October 1999 - 08:40 AM
If i'm creating a game, and I want to have a Save game option, I would like to:
1) Create a file
2) Save into the file data like Units X and Y
After I would like to add an Open game:
1)Open a file
2)Read data and convert it to the game
3)Position the units in the X and Y.
I hope someone can help me now...
#4 Members - Reputation: 122
Posted 01 October 1999 - 09:46 AM
First you have to declare a struct containing all the data you want - it could look like this:
code:typedef struct _Something
{
// Data goes here...
} SOMETHING;
After the struct has been filled with the data you need, you can save it like this:
code:
SOMETHING GameData;
//...
int handle = open ("FileName.sth", O_CREAT | O_WRONLY | O_BINARY);
if (handle != -1) write (handle, &GameData, sizeof (GameData));
close (handle);
If you want to read the data again, just do the following:
code:
int handle = open ("FileName.sth", O_RDONLY | O_BINARY);
if (handle != -1) read (handle, &GameData, sizeof (GameData));
close (handle);
Ok, maybe not the best solution, but working.
You should have a look at the descriptions of these functions contained in your compiler's help. There are always examples that show you this stuff better then I do here.
Have a nice day... Andreas
#5 Members - Reputation: 122
Posted 02 October 1999 - 01:53 AM
I have tried the code that you have gaved me and I've got a bunch of errors... I have checked the MSDN Library, but there are lots of functions called open... What can I do?
Thanks!
[This message has been edited by BenB (edited October 02, 1999).]
#8 Members - Reputation: 122
Posted 05 October 1999 - 01:00 AM
code:FILE *MyFile;
MyFile = fopen("c:\whereyouwantit", "wb"); // or rb, wt, rt etc.
//write to the file with such thing as fwrite
fwrite(MyFile, "this is what you write");
//or
fwrite(MyFile, "point 1: %d", x);//reading is with fread
//close
fclose(MyFile);
//or if you have multiple files op
_fcloseall(); // to close them all at once
though the help can be helpful to you, in this case....
you can also use functions like
fputc or the like
------------------
Dance with me......






