Archived

This topic is now archived and is closed to further replies.

BenB

Using File

Recommended Posts

MikeD    158
If you want help I think you're going to have to be more specific.
Do you mean you're programming and you're trying to save, in which case what programming language are you using?
Otherwise......

Share this post


Link to post
Share on other sites
BenB    122
Hi!
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...

Share this post


Link to post
Share on other sites
Melo    122
Here comes a solution for C++:

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

Share this post


Link to post
Share on other sites
BenB    122
What files do I have to include?
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).]

Share this post


Link to post
Share on other sites
Melo    122
No problem...
code:

#include fcntl.h
#include io.h


Should work fine, now...
Bye!

[This message has been edited by Melo (edited October 02, 1999).]

[This message has been edited by Melo (edited October 02, 1999).]

Share this post


Link to post
Share on other sites
BenB    122
Hello!
I have no idea how to save data in a file. can someone link to an article, or tell me how to do it?
Thanks!

Share this post


Link to post
Share on other sites
CJ    122
Why not use:

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......

Share this post


Link to post
Share on other sites