Thread: Text File database

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    28

    Text File database

    Ok, I wanted to make a pretty basic program to make a database, using text files.

    I have finished it now, it will compile, etc, but when you enter the details of some person, somehow, no text file is created.
    Maybe one of you can see what I'm doing wrong:

    [Edit]

    I cut out the *Wrong* code, you can see the (Semi)final thing below.

    I attatched the old code in a text file, might you want to see.

    [/Edit]
    Last edited by krageon; 12-30-2005 at 01:41 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I have finished it now


    The filename contains illegal characters. use stringstream to convert the employee number into a string, something like this:
    Code:
                    cout<< "Id Number?\n:"; // Seems pretty self-explanatory to me.
                    cin>> employee.id_no;
    		stringstream s;
    		s << employee.id_no;
    		s >> _id;
    Last edited by Ancient Dragon; 12-30-2005 at 10:18 AM.

  3. #3
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    just to expand a little more on what Ancient Dragon said,,,

    the reason you are getting illegal characters in the file name is because when you say...
    Code:
    string half_filename = _id + txt;
    you are getting the ascii char value of _id instead of the actual int value you want..

    so if the id is 44 then when the file is created it is Member,.txt and if id is 33 then you get Member!.txt .. get it..?

    otherwise it is working.. well the entry creation is.. i ran it on linux and it runs with no problems.. creates the files and info in them..

    good luck.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Thanks a lot for all your help, I'll rewrite it now

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    I get an error, about stringstream:

    40 C:\Dev-Cpp\DatabaseProject.cpp aggregate `std::stringstream s' has incomplete type and cannot be defined
    How does stringstream work? Just curious

    to Xhi: But even so, shouldn't it still make a *.txt file? On this ( Win 2000 ) Machine, it won't make anything, no text file, nothing
    Last edited by krageon; 12-30-2005 at 12:52 PM.

  6. #6
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    you need
    #include <sstream>

    and a summary of how i would use a stream here is like..
    Code:
    int id=0;
    stringstream sstr;
    string filename="";
    
    cin >> id;
    sstr << id;
    filename = sstr.str() + ".txt";

    >> to Xhi: But even so, shouldn't it still make a *.txt file? On this ( Win 2000 ) Machine, it won't make anything, no text file, nothing

    i forgot in my last post.. i wanted to tack on that if you want to test your current code you should be able to enter an id between 48 and 57 and it should work..

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Thanks a lot, my file creation is working now, I will adapt the file reader in a moment.

    The reason I'm saving the choice via a detour ( first into a struct, and then converting ) is because when I started making this, I had just learned about structures, and wanted to do something with them. It just happend to get a little bigger then expected :P

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Would it work if I attatched a string like c:/Dev-cpp/database/ to my filename, and would that store it in a subdirectory?

  9. #9
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> Would it work if I attatched a string like c:/Dev-cpp/database/ to my filename, and would that store it in a subdirectory?

    IMO, its not a good idea to put the entire path in the string.. but you can just put the subdir in there..

    so instead of prepending
    Code:
    c:\Dev-cpp\database\
    just prepend
    Code:
    database\
    then it is flexible, so if you want to run the executable from My Documents it will create a subdir under My Documents called databse.. see?

    also.. which way are your slashes going in that exmaple.. looks like you normally are on *nix??

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    28

    Thumbs up

    lol

    I work with both, and I prefer Linux ( Fedora Core ), right now I am at my grandmothers house, so I'm using a diffrent PC then normal, at home I just reformatted my PC, but I'll put in a Win XP / Linux dualboot soon

    Thanks for your time, you've been a great help to me.

    [Edit]

    Besides, is a forward slash not the safest thing to use in C++ anyway?


    [/Edit]

    [Edit]

    Thanks again !!!!

    [/Edit]

    [Edit]

    Again, to avoid heaps of code, I will post the .txt file with the 'Classic' Database thingy.
    Last edited by krageon; 12-30-2005 at 05:07 PM.

  11. #11
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    yesss. i see i recieved a special mention.. im speechless.. good thing i have somthing prepared..
    Code:
    oh my, i have been dreaming of this day for so long.. 
    i have so many people to thank. my parents.. my manager .. 
    all of my friends on the west side, 
    you stuck with me through thick and thin and...
    my well never mindd.. enough of that..

    as for the code.. so far so good.. ill leave the nitpicking to you, now that its working you can optomize

    one little thing though.. be sure to
    #include <string>
    to avoid any problems when moving between compilers..

    good luck..

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    Thanks again for the great help.

    Nice speech!! :P

    I was thinking of expanding it, not sure what to add though

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    28
    I have stumbled across another problem:

    When I try to run

    [Edit]
    Changed, new code down below.
    [/Edit]

    This, and, when running, select option 2 ( Read from file ) and then say that I dont know what number to choose, then it should give me a listing of things in the /database folder, but it doesn't, it just skips right throuh that part, and goes on asking me if I want to quit, without naming the diffrent *.txt files in the folder.

    ( I know it says it's finished, thats because it was, I just began tweaking it. )
    Last edited by krageon; 12-31-2005 at 05:52 AM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's your problem. This is undefined.
    Code:
    number = number++;
    ->
    Code:
    number++;
    Code:
    else if ( answer != 1 ) {
    ->
    Code:
    else
    Code:
                       cin.ignore();
                       cin.get();
    See the FAQ on how to clear stdin.

    Instead of using a 1/4 symbol for end-of-record, you might want to use a NULL character.

    BTW, you never close file_list.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your break in the case 2 isn't in the right place. It's in the second if. If the first if is true, execution will continue into the default case, printing "wtf". Solution: move the break to outside the if.

    And BTW, you can split strings to make the listing easier to read, and the preprocessor will join them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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, 03:17 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04: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