Solved

persistant data storage

Posted on 2003-11-21
11
303 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ finding a sting in a char* string from a text file 3 120
C++ question 3 70
Printing the elements of a set declared inside a map in C++. 3 52
Least Squares Curve Fitting 4 96
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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