Solved

persistant data storage

Posted on 2003-11-21
11
301 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Dynamically allocate memory 9 56
How to convert MFC APP to Win32 APP. 19 69
c++ syntax question 9 44
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

19 Experts available now in Live!

Get 1:1 Help Now