Thread: Need help on String Searching.

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Need help on String Searching.

    Basically in this program I am opening a text file (most likely a book or something).

    I want the user to enter in a word or phrase they wish to search for. Then I want to output the file that was searched and what word or string was originally searched for.

    I want to show each line of where the string searched occurs and show the line number in the file and show where in the line the string is located.

    After the last line has been read, I want a report of how many times the entered word was found and how many total lines are in the file.

    I want all of that written to a seperate file.
    ------------------------------------------------------------------------------------------


    However, Im having trouble reading in the values and actually having it " Find" my word in the text.

    This is what I have thus far :

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string stringLine;
    string userDefinedSearch;
    
      
    
    
    ifstream Istream;
    ofstream Ostream;
    
    Istream.open("shakespeare.txt");
    Ostream.open("data.txt");
    
    
    cout << " Please enter what you are looking for : " ;
    getline(cin,userDefinedSearch);
    
    while ( Istream != 0)
    {
    
    
    
    cout << userDefinedSearch << endl;
    
    
    
    
    
    
    
    
    }
    
    
    
    Istream.close();
    Ostream.close();
    return 0;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Ok, this is for a word. To show you one method of how to count and access, ect.
    This will not work for a phrase though.
    For the phrase you will have to use the getline function like you where, and then probably use the find string function and check every getline input for the phrase:
    http://www.cppreference.com/cppstring/find.html


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string stringLine;
    string userDefinedSearch;
    
      
    
    
    ifstream Istream;
    ofstream Ostream;
    int counter = 0;//For counting total occurences
    
    int line = 1; //For counting the line it occurs on
    char check; //For using peek, since the >> (and getline) operator ignores the new line character
    
    
    Istream.open("shakespeare.txt");
    
    if (Istream.fail() ) 
    {
    	cout << "Error opening input file!";
    	cin.get();
    	return 1;
    }
    
    Ostream.open("s_rlpruett.txt");
    if (Ostream.fail() ) 
    {
    	cout << "Error creating output file!";
    	cin.get();
    	return 1;
    }
    
    
    
    cout << "Please enter the word you are looking for : " ;
    	cin >> userDefinedSearch;
    
    while ( ! Istream.eof() ) //There is better methods than this. I personally like tacking on ###END_OF_FILE### 
    //onto the end of the file (twice on two different lines) and stoping when it get's to that.
    {
    
    
    	Istream >> stringLine;
    	check = Istream.peek();//This puts the next char into check (and will also put new line char into it)
    
    
    	if (stringLine == userDefinedSearch) 
    	{
    		
    		counter++;
    		Ostream << counter << ") occurence is on line: " << line << endl;
    		//Outputs occurence number and which line it is on.
    	}
    
    
    	if (check == '\n') line++; //This is how I use to check if it is a new line, after each istream >>
    	//see if the next character is the new line char
    
    
    
    
    
    }
    
     Ostream << counter << " occurences of the word: " << userDefinedSearch;
    
    
    
    
    Istream.close();
    Ostream.close();
    return 0;
    }
    *edit*
    With this method, empty lines are not counted as lines since the >> will skip them and go to the next line with data/
    Last edited by Enahs; 11-21-2005 at 09:53 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Still having trouble actually showing the output to the screen.

    Not familar with the peek command and a few others. Is there another way of implementing those.

    Presently the code wont read in all the text and display the values/


    Not sure of the getline usage also .


    Right now I get an infinite Loop of whatever Word I enter.


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string stringLine;
    string userDefinedSearch;
    
      
    
    
    ifstream Istream;
    ofstream Ostream;
    int counter = 0;                                                   //For counting total occurences
    
    int line = 1;													  //For counting the line it occurs on
    char check;														 //For using peek, since the >> (and getline) operator ignores the new line character
    
    
    Istream.open("shakespeare.txt");
    
    if (Istream.fail() ) 
    {
    	cout << "Error opening input file!";
    	cin.get();
    	return 1;
    }
    
    Ostream.open("s_rlpruett.txt");
    if (Ostream.fail() ) 
    {
    	cout << "Error creating output file!";
    	cin.get();
    	return 1;
    }
    
    
    
        cout << "Please enter the word you are looking for : " ;
    	getline(cin,userDefinedSearch);
    
    while (Istream != 0)										//There is better methods than this. I personally like tacking on ###END_OF_FILE### 
    																//onto the end of the file (twice on two different lines) and stoping when it get's to that.
    {
    
        
    	Istream >> stringLine;
    	check = Istream.peek();
    	
    
    	if (stringLine == userDefinedSearch) 
    	{
    		
    		counter++;
    		cout << userDefinedSearch << endl;
    		Ostream << counter << ") occurence is on line: " << line << endl;
    																			//Outputs occurence number and which line it is on.
    	}
    
    
    	if (check == '\n') line++;												//This is how I use to check if it is a new line, after each istream >>
    																			//see if the next character is the new line char
    
    
    
    
    
    }
    
     Ostream << counter << " occurences of the word: " << userDefinedSearch;
    
    
    
    
    Istream.close();
    Ostream.close();
    return 0;
    }

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Still having trouble actually showing the output to the screen.
    Change the Ostream to cout then.
    Code:
    Ostream << counter << " occurences of the word: " << userDefinedSearch;
    Outputs to the file s_rlpruett.txt like I thought you said you wanted?
    Code:
    cout << counter << " occurences of the word: " << userDefinedSearch;
    Will output to the screen.
    When you run my code, and for the file I created when you open the s_rlpruett.txt you get the following results:
    Code:
    1) occurence is on line: 1
    2) occurence is on line: 5
    3) occurence is on line: 20
    4) occurence is on line: 32
    5) occurence is on line: 32
    ..
    ..
    
    34) occurence is on line: 145
    34 occurences of the word: semper
    Not familar with the peek command and a few others. Is there another way of implementing those
    Yes, but the standard commands are there for a reason. Very usefull!
    For strings:
    http://www.cppreference.com/cppstring/
    For input and output (cin, cout ect):
    http://www.cppreference.com/cppio/

    Alternatively though. Every time you call getline you know it is moving to a new line. So when you use get line you can dump the char and the peek (again my code was just for words not phrases) and increment the line counter by one. That is, increment the line character by one every while loop, because you know if it is going around again it is on a new line.


    Not sure of the getline usage also
    You have it right for the user to input.



    Code:
     getline(input_Stream,string_it_goes_in)
    So in the while loop change the
    Code:
    Istream >> stringLine;
    Which is just line cin >> stringLine (but from the file and not the user) to
    Code:
    getline(Istream,stringLine);
    Right now I get an infinite Loop of whatever Word I enter.
    Of course.
    Explain to your self in words this code you have:
    Code:
    while (Istream != 0)
    What do you think that does?

    But then in your while loop you will have to search the string:
    http://www.cppreference.com/cppstring/find.html
    for the word or phrase the user entered and then increment the counter by one, ect.

    *edit*

    Go back through my code and make sure you understand what is going on (you can ignore the peek since you will not need it when you use getline).
    To create the program you want (search for phrase) it only requires dumping the peek, change the cin>> to the getline (and the filestream >> to the getline) and use that find link and you are done). We are talking deleting the peek stuff, deleteing the if (check == '\n') and changing the if statement and you are done.
    Last edited by Enahs; 11-21-2005 at 10:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 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. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM