Text File database

This is a discussion on Text File database within the C++ Programming forums, part of the General Programming Boards category; Ok, with your help, I made it work, only not as it should. It does show file names, but it ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Ok, with your help, I made it work, only not as it should.

    It does show file names, but it doesn't increment the file number with 1 ( Member1.txt -> Member2.txt , etc. )

    I might be doing something wrong with the if statement, but I cant really c what.

    Code:
    Removed, no longer neccesary.
    (( I only put in the 2nd case, otherwise it might be a bit long ))

    Also, why should I want to clear stdin?

    Thanks for all your help so far
    Last edited by krageon; 12-31-2005 at 09:32 AM.

  2. #17
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    couple of things..

    you seem to have doubled everything in the do.while...
    incrementing number twice, and opening the file twice (while only closing once)..
    and i dont think the way you are using sstream will work.. and to do this you will have to clear the sstream every iteration.. so somthing like this maybe inside the do.while..
    Code:
    convert.str("");
    convert << number;
    number_string = convert.str();
    filename = name + number_string + txt;
    file_list.open(filename.c_str());
    if(file_list.is_open()){
        cout << filename << "\n";
        file_list.close();
    }
    one thing i would do is to create a db file that keeps track of all of these entries.. so you dont have problems when you skip an id number or when you delete members..

    so the db file would contain stuff like ..
    the total # of Members -- so you know when you can stop scannign for members
    all the member ids.. so you can read a member out of the db file and then go open up the member data file.. then if there is a problem opening the data file report it as an error..

    just a thought..

  3. #18
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    oops

    >> It does show file names, but it doesn't increment the file number with 1 ( Member1.txt -> Member2.txt , etc. )

    the reason it did not seem to be incrementing was because of the way you were using the sstream..

    this is from the top of your program where it works
    Code:
    sstr << id;
    string filename = name + sstr.str() + txt;
    this is from the bottom where it does not ..
    Code:
    number++;
    convert << number;
    convert >> number_string;
    filename = name + number_string + txt;
    could (should) have been
    Code:
    number++;
    convert << number;
    filename = name + convert.str() + txt;
    see the difference.. you threw in an extra string which didnt hurt, but then stopped using sstr.str() which you do need.

    i wnated to throw this in the other one but forgot..

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Ok, I hadn't seen your post, so I began tweaking this myself.

    [Edit]

    I put code here earlier on, but I'm gonnan rewrite the program for a Db.txt file anyway, so I'll just skip the new search thing, and replace it with the DB file.

    Great idea, thanks

    Lol, I see what you are saying, I had absolutely no idea of that

    [/Edit]

    *snip*

    Your Db idea is great, I'll think about how to add that.

    Ideas:
    Code:
    /* 
       30-12-05 20:38
       Thanks people of the cboard forum!!!
       Special thanks:
               xhi
               xhi
               xhi
               xhi
               *Repeat 200000 times*
               Me
    */
    /*
      Ideas to implement:
            DB.txt file, which includes:
                   Errors
                   Member names + id's ( So the search routine can be skipped altogether. )
    */
    Last edited by krageon; 12-31-2005 at 09:49 AM.

  5. #20
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> here are still a few minor problems, but thats just because if I work too long on a project, it becomes harder to see the code.

    i know that feeling.. many a project has been screwed up at 4am..

    just be careful you dont get lost.. looked like on that last bit of code you posted that you were starting to get a little off track.. ? its a simple solution.. so if you are approaching 20 lines of code in that do.while.. you should step back for a minute(s).. check out that snippet i posted two posts ago.. it will help..

    btw.. it is working except for that little bit in the do.while.. i altered it and the program works.. it reads all the member files until it comes to a break.. i had 4 member files created, 1 2 3 and 12.. it read 1,2,3 just fine.. so you are on the right path still.. just dont go wandering off of it..

  6. #21
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Thanks

    Although I think I will do It like this:

    • A DB_Mem.txt file with the memebers ( Name and Id ).
    • A DB_err.txt file with any error messages taht might be generated.

  7. #22
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Done:

    Code:
                    clrscr();
                    cout<< "Id Number?\n:"; // Seems pretty self-explanatory to me.
                    cin>> employee.id_no;
                    stringstream s;
                    s << employee.id_no;
                    s >> iid;
                    clrscr();
                    cin.ignore();
                    cout<< "Full Name?\n:";
                    getline(cin, employee.name, '\n');
                    clrscr();
                    cout<< "Age?\n:";
                    cin>> employee.age;
                    clrscr();
                    string full_filename = name + iid + txt;
                    ofstream write_file ( full_filename.c_str() ); // Below, all of the input is printed into a text file.
                    write_file<< "Name:\n"<< employee.name << "\n\n" << "Age:\n" << employee.age<< "\n\n" << "Id Number:\n" << employee.id_no << "\n\n";
                    write_file.close();
                    ofstream write_db_file ( "database/DB_Mem.txt" ); // <-- I didn't make this up.. You know who did ;)
                    write_db_file<< employee.name << "   Id: " << employee.id_no << "\n"; // Makes a DB_Mem.txt file in the database subdir.
                    clrscr();                                                             // Will Make the search func MUCH easier to write :)
                    break;                                                                // Thumbs up for me not being off track ;)
                    }
    I'll do the member lookup thing right away.

  8. #23
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    This might be getting a bit ahead of yourself, but most databases rely on some sort of encryption to protect their contents.

    Think about it like this. A company uses a database. All the information about their employees are on this database, contained within text files.

    Everyone can read the text files so in effect, their contents are public. Employee A can read information about Employee B and vice versa- Problem.

    The question is how to you make it so that employees can only access information personal to them, yet at the same time allow all employees to use the SAME database?

    Answer, encryption.

    Check out XOR encryption... It's reasonbly simple... Good luck.

  9. #24
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Hey....

    You got a point there

    I'll try, but I'm only at:
    Recursion--a function calling itself

    Of the Tutorial....

    Bah, who cares, I'll try to finish what I need for encryption, and see how far I get

    Thank you for all the great ideas, for a small side project to see what I have learned, this has already become quite big

    *Grins a very happy grin*

  10. #25
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Ok, I'm not really filled in on the XOR encryption yet, I'll come to that.

    This is the program, with filly implemented search & DB_Mem.txt.

    @Xhi This also gets around the prob of Id's 1,2,3 and then 12, since it just reads whatever is inside the text file

    Code:
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <clrscr.h> // Yesyes, a custom header thingy, but no credit 2 me, it's taken from the faq.
    
    using namespace std;
    /* 
       30-12-05 20:38
       Thanks people of the cboard forum!!!
       Special thanks:
               xhi
               xhi
               xhi
               xhi
               *Repeat 200000 times*
               Me
    */
    /*
      Ideas to implement:
            DB.txt file, which includes:
                   Errors
                   Member names + id's ( So the search routine can be skipped altogether. )
    */
    struct dbase_read {
           int id_no;
           string name;
           int age;
           };
    
    int main() {
        int x = 0;
        dbase_read employee; // This program creates a text file for each new entry,
                             // in a subfolder called database.
                             // hey, maybe I'll add a mobile phone/ house phone number function!!! :D
        while ( x != 1 ) { /*See the explanation ( At the bottom )*/
        cout<< "Do you want to:\n\n1. Create a new employee\n2. Read the Database\n\n:";
        int choice;
        cin>> choice;
        string txt = ".txt";
        string name = "database/Member";
        string iid;
        switch ( choice ) {
               case 1: {
                    clrscr();
                    cout<< "Id Number?\n:"; // Seems pretty self-explanatory to me.
                    cin>> employee.id_no;
                    stringstream s;
                    s << employee.id_no;
                    s >> iid;
                    clrscr();
                    cin.ignore();
                    cout<< "Full Name?\n:";
                    getline(cin, employee.name, '\n');
                    clrscr();
                    cout<< "Age?\n:";
                    cin>> employee.age;
                    clrscr();
                    string full_filename = name + iid + txt;
                    ofstream write_file ( full_filename.c_str() ); // Below, all of the input is printed into a text file.
                    write_file<< "Name:\n"<< employee.name << "\n\n" << "Age:\n" << employee.age<< "\n\n" << "Id Number:\n" << employee.id_no << "\n\n";
                    write_file.close();
                    ofstream write_db_file ( "database/DB_Mem.txt", ios::app ); // <-- I didn't make this up.. You know who did ;)
                    write_db_file<< employee.name << "   Id: " << employee.id_no << "\n"; // Makes a DB_Mem.txt file in the database subdir.
                    clrscr();                                                             // Will Make the search func MUCH easier to write :)
                    break;                                                                // Thumbs up for me not being off track ;)
                    }
               case 2: {
                    int know_id;
                    cout<< "Do you know the persons Id?\n:";
                    cin.ignore();
                    cin>> know_id;
                    cin.ignore();
                    clrscr();
                    if ( know_id == 1 ) {
                       cout<< "What is the Id Number of the person you want the details of?\n\nEnter Id:";
                       int id=0;
                       cin>> id;
                       stringstream sstr;
                       sstr << id;
                       string read;
                       string filename = name + sstr.str() + txt;
                       ifstream read_file ( filename.c_str() ); 
                       getline(read_file, read, '');
                       clrscr();
                       cout<< read;
                       cin.ignore();
                       cin.get();
                       clrscr();
                       }
                     else {
                       ifstream db_mem ( "database/DB_Mem.txt" );
                       string db_read;
                       getline(db_mem, db_read, '\0');
                       cout<< "These are the members:\n" <<  db_read;
                       cin.get();
                       }
                    break;
                    }
               default: {
                    cout<< "Wtf?"; // Best I could think of ;)
                    break;
                    }
               }
        clrscr();       
        cout<< "Quit?\n:"; // So you don't have to restart the program when you want to do something again.
        cin>> x; // See the while loop, if x does not equal 1, then it continues.
        clrscr();
        }
    clrscr();
    cin.get();
    }
    Last edited by krageon; 12-31-2005 at 11:25 AM.

  11. #26
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Ok, I have read the XOR encryption thing, and I think I understand the principle, but the example on that page is a little vague to me, doe anyone happen to have a clear example ( Perhaps something with comment ) so I can see how I can make XOR encryption code?

    Also, I would be very happy if that example used C++ Strings instead of C-style strings, because I have used C++ style strings throughout my program.

    TIA, you have already been a great help, I wouldn't have been able to do it without you.

  12. #27
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Also, I've noticed that if I re-make the same person twice, he/she exists twice in the DB_Mem.txt file, this is because I append all data that the program writes, but actually, I'd like to make a check to see whether or nor a person is already in the database, but I have no idea where to start...

    Bright idea, anyone?

  13. #28
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    http://www.cprogramming.com/snippets...ount=30&page=0

    http://www.cprogramming.com/tutorial/xor.html

    There's obviously more on google.

    However, I don't know about you but I plan on getting very drunk tonight. So if I were you I'd forget what you're doing and do the same.


  14. #29
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    rofl, thanks for the excellent advice

  15. #30
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    happy new year everyone!

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21