Thread: Searching problem

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Searching problem

    Hey guys, I am using code like the following ro help me identify words in a text file however if in a line there are the words "Hi my name is Kim" and I want to pick both words Hi and my from the same line how could I do this? Thanks.

    Code:
      while (file >> array)
    	   {
    	
    		if(strstr(array,"test") != 0)
    
                  }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read line by line instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    The problem is though that I dont want to identify the contents of the whole line just those two that are side by side.
    Last edited by 182; 02-16-2006 at 09:50 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont want to identify the contents of the whole line to those two that are side by side.
    Sorry, but I do not understand what you are talking about.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Sorry, what i am trying to do is to read a line from a txt file and just identify two words that are side by side and seperated by whitespace for example "hi my name is kim" if I wanted to just identify hi and my as being side by side on the same line how could this be achieved? Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read word by word, and once the first identified word is found, read the next word and check. If it isnt the second identified word, repeat the search from that point onwards.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    good idea its amazing how lack of sleep can stop clear thinking.

    Thanks again.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Just thought of something else how would you do it if you wanted to search for two words such as "hi my name is kim" and you want to find hi and kim but you do not know how many words will be seperating them in the text file?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by 182
    Sorry, what i am trying to do is to read a line from a txt file and just identify two words that are side by side and seperated by whitespace for example "hi my name is kim" if I wanted to just identify hi and my as being side by side on the same line how could this be achieved? Thanks.
    An easier way to accomplish that is to read the entire line into a string type using:

    getline(myIfstreamObject, myVarThatIsAStringType);

    Then you use a string's find() function to search the string for "hi my".

    Just thought of something else how would you do it if you wanted to search for two words such as "hi my name is kim" and you want to find hi and kim but you do not know how many words will be seperating them in the text file?
    Continuing with the above suggestion, you would use find() to search the string for "hi", and then you would use find() to search the string for "my". Of course that would also identify strings that have "my" first followed by "hi". If you only want strings with "hi" followed by "my", then when you search for "my" start searching the string from the location of "hi" to the end of the string rather than searching the whole string.

  10. #10
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    you can also use sscanf()

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I will have to look these functions up as I have never heard of them before. Thanks for the advice guys I will look into those now

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    well if you know which 2 words you want to search for, and you
    can search word by word, you could search for one first, and
    search the remaining text for the other. you could even make
    it not order dependant by comparing each word read in to a list
    you have prepared, using an array of some such. i'm not sure
    of C++ file i/o yet so i won't post sample code, but just the
    concept.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the advice guys, do you know where I can see examples of the find() function working? I have looked on google but couldn't find anything useful?

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by 7stud
    An easier way to accomplish that is to read the entire line into a string type using:

    getline(myIfstreamObject, myVarThatIsAStringType);

    Then you use a string's find() function to search the string for "hi my".


    Continuing with the above suggestion, you would use find() to search the string for "hi", and then you would use find() to search the string for "my". Of course that would also identify strings that have "my" first followed by "hi". If you only want strings with "hi" followed by "my", then when you search for "my" start searching the string from the location of "hi" to the end of the string rather than searching the whole string.

    Im still having problems with this is there anyway it could work by adapting the following code even though it uses an array?

    Code:
    char array [200];
    ifstream file;
    
          while (file >> array)
    	   {
    			if(strstr(array,"test") != 0)
               }

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by 182
    Just thought of something else how would you do it if you wanted to search for two words such as "hi my name is kim" and you want to find hi and kim but you do not know how many words will be seperating them in the text file?
    A stringstream might be a easy way to do this. It makes it easier because it will automatically skip whitespace. Here's an example which may give you some ideas.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int main()
    {   
       string filename = "file.txt";
       ifstream in(filename.c_str());
       if (!in.is_open())
       {
          cout << "Unable to open file." << endl;
          cin.get();
          return 1;
       }
       
       string line;
       bool found_hi = false;
          
       while (getline(in, line))
       //while (getline(cin, line))
       {
          istringstream iss(line);
          string word;
          while (iss >> word)
          {
             cout << word << endl;
             if (found_hi == true)
             {
                if (word == "kim")
                {
                   cout << "Found hi and kim." << endl;
                }
             }
             else if (word == "hi")
             {
                found_hi = true;
             }
          }
       }
    }

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. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM