Solved

persistant data storage

Posted on 2003-11-21
11
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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