That requires that you load the entire list while running, or that you write your own search routines to search for the user/id combination.
True but I feel that would be easier !
With a DB you simply set of a query with the username as the search string and return the "id".
As someone quoted earlier you've
deceptively missed a few other things:
1. Installation of DB?
2. Creation of DB table?
3. Code to create the DB connection?
4. Readup how to do step 1, 2 and 3 or ask someone???
I sure agree that
once you're done with all this you'll "simply set of a query with the username as the search string and return the id".
No fuss, no muss. No loading the entire list into main memory, and no self-rigged search routines. And storing new user/id combos is just as easy as finding one. A single statement, and you're done.
Here is the fuss/muss we'll have to do if we use flat file (see the code at end). Self-regged search routines? std::map gives it. map::find().
Abt the "single stmt and you're done part" as I said earlier, it's not really true.
And this includes the possibility of a new user attempting to use the same name as a current user, when the username is the primary key, as the insert will fail with a primary key validation. With that, you no longer even have to check yourself whether a new username exists or not. Simply try to add it.
See the return type of std::map::insert(). It does this for you. Again you don't write no code.
#define MAX_LINE_LEN 100
int main()
{
FILE * pFile = fopen ("user_info.txt", "w+");
if( ! pFile )
{
cout << "Could not open file" << endl ;
return 1 ;
}
for( int i = 0; i < 10; i++ )
{
char dummy[50] ;
itoa(i, dummy, 10) ;
fprintf (pFile, "%s %d\n", dummy, i);
}
rewind (pFile);
map< string, int > usrInfMap ;
char str [80];
int j;
while( EOF != fscanf (pFile, "%s %d", str, &j) )
{
usrInfMap[str] = j ;
};
fclose (pFile);
map< string, int >::iterator it = usrInfMap.begin();
for( ; it != usrInfMap.end(); it++ )
cout << it->first.c_str() << " = " << it->second << endl ;
return 0;
}
Now finally remember that I said earlier:
I would recommend not to use DB
if this is the only reason you want to use it (=>DB).