Public Group

# Visual C++ records

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

## Recommended Posts

What I'm looking for is a sample of how to keep track of records. And save them to a file. So that I can, add,edit,delete, and search threw the records. I been doing some searching on google. just haven't fount any thing that I'm looking for I did find some address book samples. Its just that they have limits of 5 or less and they write to more then one file.

##### Share on other sites
You simply need to define some format that you can easily parse from a file. This might be something as simple as a tab- or comma-delimited list or something more complex such as a hierarchical structure (eg xml) or some custom block format.

To store an arbitrary amount of records, look into std::vector, std::multi_set or std::multi_map where the primary identifying field (say, last name) is used as the sorting key.

You could also use a hierarchy of these containers to allow, for example, an arbitrary number of addresses or phone numbers on a person-by-person basis.

##### Share on other sites
ravyne2001 has the right information, but I figured I might as well throw an example out there:

#include <stream>#include <string>#include <vector>struct Record{    int id;    std::string name;    int zip;    std::string address;};std::vector<Record> records;void SaveRecords( std::string fileName ){    std::ofstream out( fileName.c_str() );    for( int i = 0; i < records.Count; i++ )    {        Record record = records.at( i );        out << record.id;        out << record.name;        out << record.zip;        out << record.address;    }}void LoadRecords( std::string fileName ){    std::ifstream in( fileName.c_str() );    while( !in.eof() )    {        Record record;        in >> record.id;        in >> record.name;        in >> record.zip;        in >> record.address;        records.push_back( record );    }}

Note: It's been a long time since I've used C++ and STL. If I mucked something up, let me know.

##### Share on other sites
Mike's example is the most basic form (essentially a return-delimited list) which works well with stream behavior, so its an excellent starting point for you.

Unfortunately, its not terribly extensible and the file (should you ever need to read it with an external program) is just a great big dumb list. If you were to add or remove a line, you might suddenly have "people" whose names are addresses, addresses are phone numbers, and phone numbers are the names of other people.

Again, its a great starting point and may meet your needs, but its not robust (just like any delimited-list style approach) which has no reliable implicit or explicit type information.

##### Share on other sites
Adding to what the others said here is another couple of options.
If you don't want to load all the records into memory at once but read/edit/write one at a time from file, you could use a hashing function.

1. Finding the position and reading a record from file is fast (constant time)
2. You don't need to load more than one record from the file into memory.

Limitations:

1. Each record must have a unique key used to calculate the records position in the file. Without knowing the key you won't be able to retrieve the record.
2. The max number of records must be known from the start.

Examples of such a key could be a string containing a bank account number, or a string containing firstname, lastname and street address concatenated together.

If your record has a fixed size, implementing this scheme is quite easy. The hashing function used to calculate the file position could look something like this (taken from Data Structures & Algorithm Analysis in C++ by Mark Allen Weiss):
unsigned long hash(const string& key, unsigned long maxRecordsTimesTwo){    unsigned long hashVal = 0;    for(int i = 0; i < key.length(); i++)        hashVal = 37 * hashVal + key[ i ];    hashVal %= maxRecordsTimesTwo;        return hashVal * sizeof(RECORD);}

Note that the maxRecordsTimesTwo should be at least twice as big as maxRecords to reduce the chance of generating the same position twice.
In other words, there is a chance that hashing algorithms like this will generate the same position based on two different keys!, even though this chance is supprisingly low as long as maxRecordsTimesTwo is big enough and the key contains *random* items.

Depending on the records and application, there is a good chance that the best choice is to use a database like sqlite. If searching among the records and record entries is a biggie, its hard to beat a SQL database.

edit:
Modified the hashing algorithm to align hashVal to recordSizes

[Edited by - pulpfist on January 4, 2008 3:19:12 PM]

##### Share on other sites
Quote:
 Original post by Mike.Popoloskiravyne2001 has the right information, but I figured I might as well throw an example out there:*** Source Snippet Removed ***Note: It's been a long time since I've used C++ and STL. If I mucked something up, let me know.

Mike, thanks for the sample. Any chance I can talk you in to adding the rest of it. I been playing with some options and well as all ways having a hard time with it.

Then display the records back;

Oh, and before I forget you don't need to add the std. I use using namespace std; at the top of my project.

And I do have a menu system setup in my project

##### Share on other sites
For a small exercise program, saving to a file is fine. For large amounts of data and modifications, a database would be a more suitable approach. SQLite comes to mind, being a small and light-weight database engine. It still uses files under the hood, but you don't have to worry about that, because SQLite takes care of that, and you get to play with a high-level interface that allows easy adding, modification and searching.

Not that SQLite is the only solution for situations like this, and perhaps it's overkill for your situation, but I thought it wouldn't hurt to make you aware of other approaches. :)

##### Share on other sites
Quote:
 Original post by Captain PFor a small exercise program, saving to a file is fine. For large amounts of data and modifications, a database would be a more suitable approach. SQLite comes to mind, being a small and light-weight database engine. It still uses files under the hood, but you don't have to worry about that, because SQLite takes care of that, and you get to play with a high-level interface that allows easy adding, modification and searching.Not that SQLite is the only solution for situations like this, and perhaps it's overkill for your situation, but I thought it wouldn't hurt to make you aware of other approaches. :)

Captain P. Thanks. I'll have to look in to that. One question. How hard is it to install and can you give me a sample of the code. Just make it simple

Joe

##### Share on other sites
Quote:
 Original post by Mike.Popoloskiravyne2001 has the right information, but I figured I might as well throw an example out there:*** Source Snippet Removed ***Note: It's been a long time since I've used C++ and STL. If I mucked something up, let me know.

Mike, I spent some time playing with it. Is this right.
************************************************************

char ch;

system("cls");
cout<<"Person Information required"<< endl;
cout<<"*******************************************\n";

cin >> record.name;

cout<<" Is this Correct Y/N : "; // This doesn't work
cin >> ch;
if (ch == 'n') goto stop;

SaveRecords("records.dat");

stop:
******************************************************

void SaveRecords( string fileName )
{
ofstream out( filename.c_str() );

for( int i = 0; i < records.Count; i++ )
{
Record record = records.at( i );

out << record.id;
out << record.name;
out << record.zip;
}
}

Thanks

Joe

##### Share on other sites
Joe, when you add the record, create the new record object, fill it with data, and then add it to the vector to store it. Also, I always put "std::" before the appropriate objects instead of "using namespace std;" for several different reasons, the main one being that it keeps the namespaces clean and separate.

I will show you a cleaned up version of the code, which appends the new record to the file instead of saving them all at once.

void add(){    Record record;    char ch;    system( "cls" );    cout << "Person Information required" << endl;    cout << "*******************************************\n";    cout << "Your Name ";    cin >> record.name;    cout << "Your Address : ";    cin >> record.address;    cout<<"Your Zip Code : ";    cin >> record.address;    cout <<" Is this Correct Y/N : ";    cin >> ch;    if( ch == 'n' || ch == 'N' )        return;    SaveRecord( record, "records.dat" );}void SaveRecord( const Record &record, std::string fileName ){    std::ofstream out( fileName.c_str(), std::ios_base::app );    out << record.id;    out << record.name;    out << record.zip;    out << record.address;}

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 28
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631407
• Total Posts
2999915
×