Jump to content
  • Advertisement
Sign in to follow this  
Tquila

Data storage C

This topic is 2588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off, I don't know if this was better placed in "General Programming" then please move it.

Anywho - I got a pretty good understanding of general programming now. I'm closing in on the exam in imperative programming ( in C ), and I've got a quite good understanding on programming in general. So I thought I'd try and make a text based RPG, and I don't really have an issue with most of the things I'll be making (functions etc), but one thing we havn't quite touched in lectures yet is data storage (more so than just touching the file I/O). My plan was to create items in structs (possibly a general item struct and then some more specified item structs for various types of items, e.g. a struct for weapons which holds min/max dmg, enhancements (which specifics would then be held in another struct) etc).

I've thought about doing it with arrays too, but structs has hit me like the best solution. However, saving these data values are obviously key, and my initial idea was to store it in a .txt or equal file, where I could then acces it through the file I/O. I may be totally off course here, as there may be better practices (please do enlighten me, only going on what I've learned so far). I realize many people write this in C++, but my main priority here is to stay in C, and I thought making a larger project would enhance my knowledge of the general software engineering principles even more (not to mention practice makes perfect and repetition gives "on the top of the head know how").

I've been looking around, but as said most of these are in C++.

TL;DR: General programming patterns for saving/retrieving data in C, and general structure of files etc of a project of this type.

If any elaboration is needed please ask.

Share this post


Link to post
Share on other sites
Advertisement
http://www.java2s.co...ingfromfile.htm
http://www.cplusplus...library/cstdio/

Just do something like this, code is not 100% but you can search for it online.


struct weapon
{
int damage;
int num_bullets;
int num_clips;

void WriteWeapon();
void LoadWeapon();
};

void WriteWeapon()
{
//open a file for writing
fprintf(out_file, "%i", damage);
//.....print other properties
}

void LoadWeapon()
{
//open file to read
[color="#7f0055"]char data[color="#000000"][[color="#990000"]128[color="#000000"]][color="#000000"];
fgets(in_file, data);
int damage = atoi(data);//convert characters to an int
}

Share this post


Link to post
Share on other sites
You probably want to use either plain text or use fwrite to save whole structs (or arrays of structs) to files.

Share this post


Link to post
Share on other sites
Use binary to write your files http://www.cprogramm...al/cfileio.html

struct Item
{
int id;
int level;
char name[64];
};

struct Item item;

//Write
FILE* pFile;
fopen_s(pFile, "filename", "wb");
fwrite((void*)&item, sizeof(item));
fclose(pFile);

//Open
fopen_s(pFile, "filename", "rb");
fread_s((void*)&item, sizeof(item), 1, sizeof(item), pFile);
fclose(pFile);

something like this.

Share this post


Link to post
Share on other sites

Use binary to write your files http://www.cprogramm...al/cfileio.html

struct Item
{
int id;
int level;
char name[64];
};

struct Item item;

//Write
FILE* pFile;
fopen_s(pFile, "filename", "wb");
fwrite((void*)&item, sizeof(item));
fclose(pFile);

//Open
fopen_s(pFile, "filename", "rb");
fread_s((void*)&item, sizeof(item), 1, sizeof(item), pFile);
fclose(pFile);

something like this.


Yes, it's going to be much easier to read and write your structures in binary than trying to convert to and from text. I'm not sure the "-S" is need for most C compilers, but in general you'll want to use:
fopen
fread
fwrite
fclose

You mention deciding between array's and structures, but you can use both;

typedef struct
{
int Damage;
int Cost;
int SwingSpeed;
char Name[64];
} tWeapon;

#define MAX_PLAYER_WEAPONS 8

// An array of weapons the player can own
tWeapon gPlayerWeapons[MAX_PLAYER_WEAPONS];
int gNumPlayerWeapons = 0;


Share this post


Link to post
Share on other sites
The only reason I advised possibly starting with text is:

it is readable to find flaws for a beginner. Can also be manipulated by hand if he needs to change anything. If he has pointers to upgrades or any pointers in the structs he won't be able to write out the structure as posted. So to the OP: if you have any pointers in those structs, you will have to grab the objects out of the pointers and reference their actual data members. If you think you might add pointers, you can still do binary, but you will have to do it each variable at a time.

Share this post


Link to post
Share on other sites
What you're looking for is a way to serialize data. The exact structure and format of a particular file is entirely up to you and can be changed fairly easily, but one challenge is serializing the data. As you've seen, there's lots of C++ libraries for this, but less C libraries. This guy had a question very similar to yours, and two of the top recommended libraries from that thread are Eet and Tpl. Note that they say Tpl can't store nested data structures, though Eet can. It all depends on how your structures are organized.

You could also write your own serialization functions for each data structure if you want, though it's up to you. One thing to be careful about that a lot of the solutions posted here in this thread have a problem with is endianness and alignment issues. Data structures can be stored differently in memory across different computers. Taking a direct copy of the memory from one computer, saving it to disk, and then trying to load it on another computer can create issues. That's why a good serialization library is important (whether you write your own or you use another existing library), as it avoids these issues.

Share this post


Link to post
Share on other sites
It definitely would be a good exercise to hone your current dev and programming knowledge.

Just throwing this out there. I don't know what your total background is. You might want to check out SQLite if you have some background with databases. It will save you a lot of trouble writing your own file formats. I use it extensively in my roguelike/RPG to store and organize game data.

Share this post


Link to post
Share on other sites
I think it would be better if you save them in a binary file, cause this way there's no parsing to do, you just read or write the structure data and you're done. Use fopen(fname, "rb";); or fopen(fname, "wb+";) to open/create a file, fread/fwrite to read and write and you should be fine. Of course don't forget fclose() when you're done.

Ex:

struct float3 {
float x, y, z;
};

float3 data;

// Write it to a new file
FILE *f = fopen(fname, "wb+");
fwrite(&data, 1, sizeof(float3), f);
fclose(f);

// Read it back from the file
FILE *f = fopen(fname, "rb");
fread(&data, 1, sizeof(float3), f);
fclose(f);

Share this post


Link to post
Share on other sites
Vortez, what if he wants to store multiple objects of potentially different types? What you posted there doesn't allow for that, plus it requires him to never recompile to program if he is ever going to read from the same file. Parsing and serialization is necessary if writing out to a binary format that's intended to last for more than one revision.

I think Mallach's idea is a brilliant one. Probably the simplest as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!