search a file?

This is a discussion on search a file? within the C++ Programming forums, part of the General Programming Boards category; im gonna write a program to keep track of some customers, and id like to know if i had a ...

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    128

    search a file?

    im gonna write a program to keep track of
    some customers, and id like to know if i had a simple
    input, containing the customers name and a few
    other items in a single line such as :

    Code:
    myfile << full_name << "  " << more_info << "   " << more_info2 << endl;
    what would be a good way to search thru the file for individual
    lines when the search could be based on any of the items, such
    as a search for full_name, more_info or more_info3

    my skill is limited, im still learning so simplness, or good
    explanation will get you brownie points :/

    Thanks in advance to all replys.

    (Operating System : Windows Server 2003 Web Edition)
    (Compiler/IDE : Microsoft Visual Studio 2003)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Do you want to be able to search by category? Or just an all out search? Do you want to search through your memory buffers? Or do you mean before you read in from the file.


    Try looking into the functions strstr and strtok

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    well, typically, you would read the entire file into some data structure instead of simply searching the file directly. then you'd perform things such as searching, updating, deleting, etc from that DS, and at some point overwriting the file with up-to-date data. to simplify things just place each piece of information on a single line. you'll need to define a class to hold each record and it would be a good idea to overload ifstream/ofstream operators for the class too, ie:

    Code:
    struct customer {
     string name;
     friend ifstream & operator >> (ifstream & stream, customer & the) {
      return stream >> the.name;
      }
     };
    
    bool get_customers(const char * file, vector <customer> & array) {
     customer record;
     ifstream in(file);
     array.resize(0);
     if(!in) {
      return false;
      }
     while(in.good()) {
      if(in >> record) {
       array.push_back(record);
       }
      }
     return true;
     }
    as far as searching for a piece of data a simple loop would suffice.
    Last edited by Sebastiani; 09-29-2004 at 06:05 PM.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Banned
    Join Date
    Aug 2004
    Posts
    128
    sounds good, but i dont know anything about vectors...
    ill have ot look into some of that stuff, but thats exactly what
    i need

  5. #5
    Banned
    Join Date
    Aug 2004
    Posts
    128
    Code:
    vector( input_iterator start, input_iterator end );
    Code:
     words|more|words|numbers|9003
    if each line was divided like that, how would i implent the above
    such as in a for loop to read each individaul peace, in a way
    that could be search for individually?

    Of if i can just read each line in and some how break it up?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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