Thread: search a file?

  1. #1

    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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    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
    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, 10: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, 10:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM