Thread: Searching

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Searching

    In our class we made a program that writes your name and such to a file. Now we need to search for a file by the person's last name. I've looked around and havent been able to find any help. Can anyone help?

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    How do you write to the file (the format, etc)?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    it was a sequential record

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Details, please. We need to know the exact content of the file that you're trying to read. Is it like ...
    Code:
    Susan Deli
    Jack Nicholson
    Bob Hammer
    .....(and so on), or
    Susan Deli | Jack Nicholson | Bob Hammer | ....(and so on), or
    whatever format it is
    EDIT: Read this thread for some inspiration
    Last edited by alphaoide; 01-22-2004 at 02:32 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    First name
    Last name
    Address
    Phone number

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Assuming you write in text mode,
    Code:
    ifstream inFile("data.txt");
    bool found = false;
    string keyName = "Chan";      // last name to be found 
    string lastName;
    
    while(getline(inFile, lastName) && found = false) {
        if (lastName == keyName) {
            found = true;
        }
    
        if (lastName > keyName) break;
    }
    You still need to add/make changes to read only last-name section of the file. It's a class assignment anyway, right?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    yeah its for class but our teacher doesnt help me and 3 other people because we are so far ahead.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by TeamGreen
    yeah its for class but our teacher doesnt help me and 3 other people because we are so far ahead.
    Well, at least he/she is better than MY teacher. Who goes: "Sup gurls, need some help? " (and simply type up the whole program for the gurls) ; and " HEY, YOU GUYS, wat r u doing on the internet? Wat? Your stuck? THINK 'BOUT IT"
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    First name
    Last name
    Address
    Phone number
    Best to treat every four lines as a single entity as long as this formatting is consistent. It simplifies your program greatly.
    Code:
    struct record {
        string first;
        string last;
        string address;
        string phone;
    };
    
    ...
    
    //
    // Retrieve a record from file (very simple impl.)
    //
    bool get_record(record& r, ifstream& fin)
    {
        getline(fin, r.first);
        getline(fin, r.last);
        getline(fin, r.address);
        getline(fin, r.phone);
        if (!fin.good())
            return false;
        return true;
    }
    
    ...
    
    ifstream fin("file.dat");
    record r;
    
    if (!fin.is_open()) {
        // Handle file open failure
    }
    while (get_record(r, fin)) {
        if (r.last == search_key) {
            // Process successful search
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM