Thread: Reading Certain Lines In File?

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Reading Certain Lines In File?

    I am making a database type program that keeps track of people. For example, employees. You can edit, add, or delete each person. My problem is reading specified lines and adding them to a listbox. Here's the file format:


    \Jon Doe
    Address: 123 ABC Ln.
    -
    \Jane Doe
    Address: ABC 123 Ln.
    -


    How do I read the lines with just the \Name Here on them and add them to the listbox?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    IMHO you should open, read the whole file to memory and close it (unless is very big ie 10Mb+). Do not reopen unless need to change data.

    Copy the data to an array of structures of the same format as the original data. Use a index file or linked list if large amounts of data.

    Otherwise need to look for the '/' in the file. Test the string after it with strcmp() or or strstr() and then edit it.

    Beware of editing text files as the size of strings varies (where as a struct saved as binary does not).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Using structs would be a good and organized way, but very memory consuming. I suggest novacain's second idea by just reading the file into a buffer and going through each line (with fgets) and testing to see if it has a '/' in front of the line. If you are looking for space conservation, do this. If you are looking for time conservation, use structs. It's up to you...
    1978 Silver Anniversary Corvette

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question fgets()...

    I am only familiar with fstream, but I know some about fgets(). Could you show a small example of using fgets() to do this?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question HuH!?

    ...
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Let's say you're using the stdio lib (some incomplete code just to show you implementation):
    Code:
    fp = fopen(szFileName, "r");
    
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        if (line[0] == '/')
            process the name
        else
            continue;
    }
    This (untested) code is pretty much how you want to go about it. Just check your string from getting the line from the file to see if it meets your requirements.
    1978 Silver Anniversary Corvette

  7. #7
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Exclamation I tried this...

    I tried this:


    dbFile = fopen("emplist.txt", "r");

    while (fgets(names, sizeof(names), dbFile) != NULL)
    {
    if (names[0] == '/')
    {
    do
    {
    fgets(names, sizeof(names), dbFile);
    } while (names[0] == '\n');
    }
    }


    SendMessage(dbList, LB_ADDSTRING, (LPARAM)100, (WPARAM)names);


    And it just gets the last line in the file. What did I do wrong?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    The reason for just getting the last line of the file in the listbox is because you reading in another line even when you already found a valid name line. In other words, you don't have to reread the line in. You are just incrementing the file counter and causing a problem (which is causing it to go to the last line, making it in the listbox). Here is the corrected code:
    Code:
    dbFile = fopen("emplist.txt", "r"); 
    
    while (fgets(names, sizeof(names), dbFile) != NULL) 
    { 
    if (names[0] == '/') 
    { 
    names[strlen(names)-1] = '\0'; // get the '\n' char out of the string
    SendMessage(dbList, LB_ADDSTRING, (LPARAM)100, (WPARAM)names); 
    } 
    }
    This should logically do it. You find a valid string (with the if test) and it's valid, so enter it in the listbox. Understand? Any more questions?
    1978 Silver Anniversary Corvette

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    How big is the data file?

    I would always use structs. Easier to read, pass and mem manage.

    You can read the file, fill in the structs and save to a binary file, say
    DB_STRUCT
    char sFirstName[64];
    char sSurName[64];
    char sAddress[128];
    long lPhoneNum;
    ect

    Then all you have to do is find the size of the file with GetFileSize(), divide thias by the size of a struct to get the number of records. You can then dynamicly alloc the mem with GlobalAlloc(), GlobalLock() it and read the file for sizeof(DB_STRUCT) record by record (or pick it up in one read to increase speed).
    Stack the LB using the struct. At close GlobalHandle() and GlobalFree() the mem.

    This is heaps easier in the long run. Even if your struct is say 20Kb (as the one I am using is for over 600 records is) dynamic mem alloc is not a problem.

    If you don't you could run into the problem I had trying to hack a 'Serious Sam' file. One small change in a file (changed 5 chars to 4) and it then told me it needed approx 12Gb of mem to run!
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 01:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM