disk file search algorithm

This is a discussion on disk file search algorithm within the C++ Programming forums, part of the General Programming Boards category; I am having a problem creating an algorithm to search a text file for a specific keyword. The text file ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    disk file search algorithm

    I am having a problem creating an algorithm to search a text file for a specific keyword. The text file is a list of customer profiles (company name, address, ect...). The program should ask for a search string, which could be the whole company name or just the first few letters of it, and search each entry in the file until it finds it. If there are multiple hits ( ie if there are two companys that start with 'american') then it gives a list of options to choose from then displays the complete company profile. The problem is that once I find the correct name the rest of the profile does not load up. And now, without changing any of the code, the program locks up right after the while loop starts, where it did not before. To save space I'll only list the parts of the program that are not working correctly.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int viewprof();     // Function to view a customer profile
    int findprof();     // Function to find a profile from disk
    
    struct customer     // define the structure for customer information
    {
        char custname[25], custaddress[25], custcity[15], custstate[3];
        int custzip, custroute, custcloset;
        char custcontact[20], custphone[13];
    };
    
    customer custinfo;  // define an instance of struct customer
    
    int main()
    {
         //.......
    }
    
    int findprof()
    {
        int y=0;              // controls where to look in disk file for structs
        int tempfileloc[5];   // location of structs in disk file
        int z;                // selects which customer profile to load if multiples
        int p;                // size of individual customer profile
        int a=0;              // var for tempfileloc array members
        string name;          // search variable
        char tempname[25], tempaddress[25];
        system("cls");
        cout<<"enter search name : ";
        cin>> name;
        int namelength=name.size();
        for (int i=0; i<=namelength; i++)
        {
            // change name to all cap's
            name[i]=toupper(name[i]);    
        }
        ifstream fcust("c:\\dispatch_planner\\cust_data.txt", ios::binary | ios::in);
        if (fcust.fail()) {cout<<"\a";return 0;}
        while (!fcust.eof())
        {
            p=(y*sizeof(custinfo))-(y*3);  
            fcust.seekg( p, ios::beg );
            if (!fcust.eof())
            {
                fcust.read(tempname,25);                      
                int x=0; int match=1;
                for (x=0; x < namelength; x++)
                    {            
                        // if a nonmatching character is found, 
                        // unflag 'match' and break out of for loop
                        if (name[x] != tempname[x]){match=0; x=x+20;}
                    }
                if (match==1)
                {
                    // if 'match' is still flagged, print name and address
                    // and mark struct location in file location array
                    fcust.read(tempaddress,25);
                    tempfileloc[a]=p;
                    cout<<a+1<<">  "<<tempname<<"  "<<tempaddress<<"\n";
                    a++;
                }
                y++;
            }
            else break;    
        fcust.close();
        }
        if (a==0) {cout<<"\nNo matching files to display!\n";}
        else {cout<<"\nSelect profile number";}
        cout<<"\n<0> to exit           : ";
        cin>>z;
        if (z != 0) 
        {
            ifstream fcust("c:\\dispatch_planner\\cust_data.txt", ios::binary | ios::in); 
            fcust.seekg(tempfileloc[z-1], ios::beg);
            fcust.read(custinfo.custname,25);
            fcust.read(custinfo.custaddress,25);
            fcust.read(custinfo.custcity,15);
            fcust.read(custinfo.custstate,3);
            fcust.read(reinterpret_cast<char*>(&custinfo.custzip), sizeof(int));
            fcust.read(reinterpret_cast<char*>(&custinfo.custroute), sizeof(int));
            fcust.read(reinterpret_cast<char*>(&custinfo.custcloset), sizeof(int));
            fcust.read(custinfo.custcontact,20);
            fcust.read(custinfo.custphone,13);   
            fcust.close();
        }
        return z;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Without really looking too deep, I have to ask, why are you mixing C++ strings with C strings (character arrays)? If you made them all C++ style you could do this quite easily with the substring function.
    Sent from my iPadŽ

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    simplify things by writing/reading the entire structure instead of the members individually, and separate that code from the search algorithm. once the data is loaded from the database, you can appy any sort of algorithm you wish without having to deal with the messy details of file IO.
    Code:
    #include <cmath>
    #include <complex>
    bool 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
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Well, I'm comverting the C code to C++ code. Doing one piece at a time and just have not got to everything yet when this little bug came up out of nowhere. ( i wonder if that could be part of my problem? )

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    This is a very easy program to write from scratch. Personally, with a program this size, I don't know why you're worried about converting C code. Just rewrite it. It's much easier.

    Here is an example of a search program using C++ strings and substrings:
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
        std::string sWord;
        std::string chkWord;
        
        std::cin >> sWord;
        
        while (std::cin >> chkWord) {
           for (int i = 0; i < 1 + chkWord.length() - sWord.length(); i++) {
             if (chkWord.substr(i,sWord.length()) == sWord)
                std::cout << chkWord << std::endl;
           }
        }
        return 0;
    }
    Output:
    bo The boy with the boring book was bold
    boy
    boring
    book
    bold
    ly the really tall man was lying
    really
    lying
    Last edited by SlyMaelstrom; 02-12-2006 at 10:38 AM.
    Sent from my iPadŽ

  6. #6
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Hey, Thanks for the help. That gives me some good ideas to work with.

    Also, that little snippit of code above is a small part of a VERY, VERY large program written in C. I am in process of learning C++ now and I figured the best way to do it is to redo a large scale program that I did in C a while back into C++. Instead of rewriting the entire program I was just trying to update the existing one. Maybe I should just do a complete rewrite.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

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