Searching txt files (another file i/o qn)

This is a discussion on Searching txt files (another file i/o qn) within the C++ Programming forums, part of the General Programming Boards category; Im really pants aint i??? ok - i asked a while ago how file i/o works and i got the ...

  1. #1
    Registered User Catif's Avatar
    Join Date
    May 2002
    Posts
    9

    Searching txt files (another file i/o qn)

    Im really pants aint i???

    ok - i asked a while ago how file i/o works and i got the prefect response - thanks fellas.

    I got another query now however.......

    ive got a program that enters details into a formatted output text file. I now want to search that text file for a match to certain criteria (surname to be exact) and recall the set of details for that person so they can be edited and then appended to the file.

    Im using a struct to specify the data formats i want thus:

    Code:
    struct EditDetailsType
    	{
    		int iBookingNumber;
    		int iPrice;
    		string sSurname;
    		string sFirstName;
    		int iHouseNumber;
    		string sRoad1;
    		string sRoad2;
    		string sCounty;
    		string sPostCode;
    		string sDestination;
    		string sFlightNumber;
    	}Recall;
    i know most peeps dont use the string operator but im having to se it as part of my course and it works well enough on Borland C++ 4.5

    The output for the file once the details have been entered is thus:

    Code:
    	ofstream file;
    	file.open("booking.txt", ios::app);
    	if (file.fail())
    	{
    		cout << "File Open Failed (booking.txt). Program Halted";
    		int iStatus = 0;
    		exit(iStatus -0); // this will stop the program
    	}
    	else
    	{
    		file << "\n";
    		file << "BOOKING #:     " << Booking.iBookingNumber << "\n";
    		file << "CUSTOMER NAME: " << Booking.sFirstName << " " << Booking.sSurname << "\n";
    		file << "ADDRESS:       " << Booking.sRoad1 << Booking.sRoad2 << "\n";
    		file << "               " << Booking.sCounty << "\n";
    		file << "               " << Booking.sPostCode << "\n";
    		file << "PRICE:         " << Booking.iPrice << "\n";
    		file << "DESTINATION:   " << Booking.sDestination << "\n";
    		file << "FLIGHT #:      " << Booking.sFlightNumber << "\n";
    		file.close();
    	}
    Id appreciate it if someone could tell me how to search the file ive created for the Booking.sSurname field.

    Thanks in advance (and i sure as hell hope this makes sense)

    Cat

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    I got a solution if you don't need to have your file looking like that, if you could have your file in binary mode you could do a loop and just extract each booking-struct, and check its surname.

    if you have the file looking like yours it will be worse, you have to get each line in the file, and look if the line contains "CUSTOMER NAME:". If it does, take the last word of that line and compare it to your search key.

    to sum:
    method1 - if the file has 10 bookings, you do 10 read, and 10 checks if the surname match.
    method2 - if the file has 10 bookings and each booking is 8 lines, you do 80 read, 80 checks to see if the line is "CUSTOMER NAME:" line, and 10 checks to see if the name is a match...
    On the other hand, you can optimise method 2 if you know that customer name will be every 9th line or something, but then you can't add another param in the struct without rewrithing the search function...

    if you know the file is going to be small, just load the whole thing into memory and work with it there.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    hi,

    yeah i think what ninebit's trying to say is, you use the ifstream::read() and ifstream::write() function,

  4. #4
    Registered User Catif's Avatar
    Join Date
    May 2002
    Posts
    9
    OK thanks fellas - ill give it a blast and see what happens - ill let ya know

    If anyone else has any knowledge they want to pass to dumb old me about this pls post

  5. #5
    Registered User Catif's Avatar
    Join Date
    May 2002
    Posts
    9
    Ok total and utter brain failure has occured

    Anyone know a list of sites with tutorials about file streaming coz its really starting to annoy me (the ones here are good but not as indepth as i want)

    Thanks

    Cat

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This looks like a case where using a comma separated file, or some other symbol not likely to be used in the struct data members would be useful. That way you can use getline with a comma (or whatever) as the delimiter.

    Code:
    //declare C++ style struct type
    struct EditDetailsType
    {
       int iBookingNumber;
       string sFirstName;
       string sSurName;
       //etc
    }
    
    //declare array of EditDetailsType
    const int MAX = 10;
    EditDetailsType Booking[MAX];
    
    //write array to file after obtaining data
    for(int i = 0; i < MAX; i++)
    {
       file << "BOOKING #:     " << ',' << Booking[i].iBookingNumber << "\n";
       file << "CUSTOMER NAME: " << ',' << Booking.sFirstName << ',' << Booking.sSurname << "\n";
    //etc
    
    File will look like this:
    
    Booking #:         ,12345
    CUSTOMER NAME: ,Joey,Brown
    //etc
    
    //now to read in the file do this:
    EditDetailsType Recall[MAX];
    
    char dummy[80];
    ifstream fin("filename");
    for(i = 0; i < MAX; i++))
    {
      //read and discard up to first comma
      fin.getline(dummy, 80, ',');
      
      //read in booking number
      fin.getline(Recall[i].iBookingNumber, 80, '\n');
    
      //read and discard up to next comma
      fin.getline(dummy, 80, ',');
    
      //read in FirstName
      fin.getline(Recall[i].sFirstName, 80, ',');
    
      //read in SurName
      fin.getline(Recall[i].sSurName, 80, '\n');
      
      //evaluate SurName
      if(Recall[i].sSurName == "Brown")
      {
          //do something
          cout << Recall[i].iBookingNumber << " is for a " << 
                  Recall[i].sFirstName << ' ' << Recall[i].sSurName << endl;
       }
        
        //etc.
    Last edited by elad; 05-13-2002 at 12:40 PM.

  7. #7
    Registered User Catif's Avatar
    Join Date
    May 2002
    Posts
    9
    Thanks Elad - it looks right, my other resources say its right............ BUT

    Borland C++ compiler (4.5) says "Could not find a match for 'istream::getline(int,int,char)' in function DisplayBookings()"

    the *wonderful* help file that comes with the compiler says:

    "No C++ function could be found with parameters matching the supplied arguments. Check parameters passed to function or overload function for parameters that are being passed."

    As itterated before im prolly the biggest newbie in the world and thus this means absolutly nothing to me - aint yall glad theres someone out there who is more st00pid than you (ie: me)

    ill let ya know how i get on
    Last edited by Catif; 05-13-2002 at 04:17 PM.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yup, I goofed blindly thinking iBookingNumber was a string, too. Use:

    fin >> Recall[i].iBookingNumber;
    fin.ignore();

    instead of

    fin.getline(Recall[i].iBookingNumber, 80, '\n');

    The call to ignore() is to remove the newline char which will be left in the input buffer by the >> operator and which will then be put at index 0 of the next string if not removed. Since the next string is going to be disregarded in this set of code, it probably doesn't matter here, but it matters a lot at other times, so I routinely use it when mixing >> and getline() in the same program.

    Also, you probably need to use a different version of getline() that places strings in an STL string rather than a C_style string like I have coded. I have forgotten what that syntax is, but it is close to this, if it's not this. You can look into that issue. The algorhythm for reading the file, finding the SurName, and analyzing the SurName should be the same however.

  9. #9
    Unregistered
    Guest
    you wouldnt happen to be using the awful Lawrenceville Press book?

  10. #10
    Registered User Catif's Avatar
    Join Date
    May 2002
    Posts
    9
    Originally posted by elad
    Yup, I goofed blindly thinking iBookingNumber was a string, too.
    I wish my blind goofing would still work

    Thanks for the help - its working fine now

    To Mr Unregistered (post above this one):
    no i aint using that book - im having to use very pants notes written by my course tutors in the mid 1990s

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O - Hash files
    By I BLcK I in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2009, 02:30 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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