Solved

persistant data storage

Posted on 2003-11-21
11
300 Views
Last Modified: 2010-04-02
My program needs a database of some kind to store data that I want to keep between sessions.  Something like SQL would be a bit of an overkill I think, as I wont be storing much more than 100-200 records.  Is there any sort of classes out there I could use to store my data in a file, or do I need to make my own?
0
Comment
Question by:lwinkenb
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9801522
Whyt about www.mysql.org? You still need to write the whole serializing code that does the job on your side, but that's inevitable.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9801716
It would not be that hard to make your own.

I recomend you use a std::vector or std::map container to store the data at run time, and save it to a file when your program exits.

Do you need to share the data between multiple running threads or executables?

If so, what is your OS?
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9801717
hmm, If I used mysql, would I have to have the users of my application download and set up a mysql database server?

I'm looking for something that I can use to just store my data in a flat file.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9801727
FYI:
If you're target is windows, and your data is simple, you can use the INI API functions.
GetProfileString
WriteProfileString

GetPrivateProfileString
WritePrivateProfileString
GetPrivateProfileInt

I've used these before for storing simple data.
You can use a key name that has a number at the end so that you can pull or write the data.
0
 
LVL 15

Expert Comment

by:efn
ID: 9801734
I thought Metakit looked good for this kind of thing.  I haven't used it myself, though.

http://www.equi4.com/mkoverview.html

--efn
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Author Comment

by:lwinkenb
ID: 9801748
Hey Axter, writing to the registry wont work, because I need to store a bit too much data for that.

eft >> I will give metakit a look
0
 
LVL 30

Expert Comment

by:Axter
ID: 9801753
>>Hey Axter, writing to the registry wont work, because I need to store a bit too much data for that.
How much is too much?
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 9801771
Here's a simple example:

class foo
{
public:
      char Name[32];
      int Age;
};

int main(int argc, char* argv[])
{
      foo foo_list[] = {{"Axter", 40},{"jkr", 88},{"lwinkenb", 26},{"efn", 12}};
      int i;
      char Key[99];
      char DataInt[22];
      const char KeyPrefix[] = "Key";
      const char FileNameHoldingData[] = "myfiledata.ini";
      for (i = 0;i < sizeof(foo_list)/sizeof(foo);++i)
      {
            sprintf(Key, "%s_Name%i", KeyPrefix, i);
            WritePrivateProfileString("DataSection", Key, foo_list[i].Name, FileNameHoldingData);
            sprintf(Key, "%s_Age%i", KeyPrefix, i);
            sprintf(DataInt, "%i", foo_list[i].Age);
            WritePrivateProfileString("DataSection", Key, DataInt, FileNameHoldingData);
      }

      foo foo_fetch[9] ;
      
      for (i = 0;;++i)
      {
            sprintf(Key, "%s_Name%i", KeyPrefix, i);
            GetPrivateProfileString("DataSection", Key, "", foo_fetch[i].Name, sizeof(foo_list[i].Name), FileNameHoldingData);
            sprintf(Key, "%s_Age%i", KeyPrefix, i);
            foo_fetch[i].Age = GetPrivateProfileInt("DataSection", Key, -1, FileNameHoldingData);
            if (!strlen(foo_fetch[i].Name) || -1 == foo_fetch[i].Age) break;

            cout << foo_fetch[i].Name << ", " << foo_fetch[i].Age << endl;
      }

      system("pause");
      return 0;
}

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9801804
100-200 is not significant number for database. Text file or Registry (or some combination of them) will be enough. At runtime you can keep all the data in the memory.
Consider also mixed solution: some text data is kept in the text file, ini file or Registry, and binary data (like images) is kept in the binary file, like this:

Name = John
Image =  1.jpg
...
Name = Bill
Image =  10.jpg

This list may be loaded to memory and images are loaded only if necessary.

The important thing is whether you allow to run number of instances of your program simultaneously. If yes, prefered solution should be simple desktop database or Registry (which is updated immidiately).
0
 
LVL 3

Expert Comment

by:RJSoft
ID: 9804130
Axter you have a sample handy of using vector?

RJ
0
 
LVL 30

Expert Comment

by:Axter
ID: 9806424
>>Axter you have a sample handy of using vector?

http://axter.com/code/file_vector/file_vector.h
http://axter.com/code/file_vector/file_vector.cpp

http://axter.com/code/file_vector/file_vector.zip

The above code is some old code I worked on a long time ago.  If I remember right, the windows version works, but there's some problems with the UNIX version.

I think I got stuck on the bug for the UNIX version, and soon lost interest in it.

That should give you an idea on how to create a persistant STL container class.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now