🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to create .dat files

Started by
5 comments, last by C++ Freak 24 years, 5 months ago
How do you create .dat files? I would like to figure that out because I want to use it to save games and keep a high score list. Visit members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
Advertisement
most *.dat files for things like high scores are just text files.

i''m not sure about files for saving games though
You use data structures (struct) to store the individual variables, and
then you can write the entire structure to a file. These data files are
usually written in binary, so you should have a hex editor to look at the
file after you write it to make sure it was written correctly when
debugging it.

For Win32, you can use the CreateFile, ReadFile, WriteFile functions.
For Dos, you can use the open, read, write functions.

Haze
Do you know any tutorials on how to do this?



Visit members.xoom.com/ivanickgames
Visit http://members.xoom.com/ivanickgames
Off the top of my head you can do something like this:

typedef struct _tagData {
int Blah;
int BlahBlah;
float BlahBlahBlahBlah;
} Data;

Data somedata;
char *ptr;

FILE * fp = fopen("datfile.dat", "wb");
if (fp == NULL) {
printf("File system barfed\n");
exit(0);
}

ptr = (char *)&somedata
for (i=0; i < sizeof(Data); i++) {
fputc(fp, *ptr);
ptr ++;
}

fclose(fp);

With more structures just do more fputc loops. With an array of data structures use (sizeof(Data) * num_elements) for the loop counter.
all files are binary and all files are text files
a bitmap is just a sieries of 0s and 1s in a row, as are all other file formats
your fooling yourself if you think that they have some special quality because of their file extension
also, their is no such thing as a dat file -- you come up with it as you wish
you could just save your game data in any manner you wish
-PoesRaven
Yes, this was my biggest problem when I first dealed with file formats. I was under the impression that there was something special about disk files, and that the file format really mattered. It really helps to realize that a file, whether it be for game data, a level, a saved game, or anything else is just a collection of bytes, almost just like variables in memory. For example, you have a struct, or a collection of structs that you store your level data in so you can read it and manipulate it in the game. If you want to load or save that level data, you just write that level data to a file, or read it from a file into a variable. Since that was probably confusing, it''s like this
struct Level{// ...};void SaveLevel(Level level){OpenFile(LevelFileName);SaveToFile(level,LevelFileName);CloseFile(LevelFileName);} 

and the same for LoadLevel except with loading, not saving. All you are really doing is storing your variables for later access on the disk. You might save an integer (4 bytes), then a char (1 byte), then a 20-byte string, then a short (2 bytes) and you would read them back in in the same order.

I know I just rambled on, probably needlessly, but I was just trying to get across the point that any data files, or any other file format you create, is just copying your variables to disk or loading them from disk, and the file format is only the order you copy that data in. If you don''t understand what I just said (I don''t know if it was actually clear) you can email me...



------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge

This topic is closed to new replies.

Advertisement