Thread: Need help with reading in files.

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

    Need help with reading in files.

    Well, Ive been struggling trying to get a few of these concepts to work using the getline command and others.


    I want to allow a user to enter any search string, including blanks.

    **Ive done this**

    I want create a final result showing what string was searched for showing each line of text where the search
    string occurs.

    **Im struggling on how I can display the entire line that the word is found on.**

    Also the line number in the file where it occurs and what position in the line the string is located.

    ** Struggling with doing this , Not sure of how to implement the .find , . size , and .substr **


    When the last string is read, I want to report the total
    number of occurrences of the string that were found and
    how many total lines were in the file.

    **Ive done this too**


    Here is my code thus far.



    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    
    
      
    
    
    ifstream Istream;
    ofstream Ostream;
    
    
    int counter = 0;                                                   
    int line = 1;													  
    int totalLines = 0;														 
    string stringLine;
    string userDefinedSearch;
    
    
    
    Istream.open("shakespeare.txt");
    if (Istream.fail() ) 
    {
    	cout << "Error opening input file!";
    	cin.get();
    	return 1;
    }
    
    
    
    Ostream.open("s_word.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)										 
    																
    {
    
        Istream >> stringLine;
    	totalLines++;
    	
    	
    
    	if (stringLine == userDefinedSearch) 
    	{
    		
    		counter++; 
    		cout    << userDefinedSearch << endl;
    		Ostream << userDefinedSearch << endl;
    		
    		Ostream << counter << ") occurence is on line: " << line << "  " << userDefinedSearch << endl;
    		cout    << counter << ") occurence is on line: " << line << "  " << userDefinedSearch << endl << endl;	
    		 
    	}
    
    
    	else 
    	{	
    	 
    	   
    	}
    	
    	
    
    
    }
    
     Ostream << counter << " occurences of the word:  " << userDefinedSearch << endl << endl;
     cout    << counter << " occurences of the word : " << userDefinedSearch << endl << endl;
     cout << "There is " << totalLines << " lines in this file." <<  endl;
     Ostream << "There is " << totalLines << " lines in this file." << endl;
    
    Istream.close();
    Ostream.close();
    return 0;
    }




    Here is a portion of the output.


    Code:
    Please enter the word you are looking for : love
    
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    love
    1283) occurence is on line: 1  love
    
    love
    1284) occurence is on line: 1  love
    
    1284 occurences of the word : love
    
    There is 939197 lines in this file.

    I just want the entire line that the word is found on displayed as well as its position in the file. Just kind of confused on how to display the entire actual line the searched word is found in. As well as the format of using the commands to find its position.

    Any help would be wonderful. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I would use getline() instead of >> operator so that you get the entire line at one time, and can easily detect one line from another. After getting the line with getline() you can use std::string's find method to tokenize it into words (look for spaces).

    Getting the exact position in the file where the string starts will prove to be more problematic. One way is to read the entire file into memory so that you can easily calculate the offset of the string from the beginning of the input buffer.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Well trying to replace the getline in my program results in compile errors. Just not sure on how to work it.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I can't see your monitor -- you need to post the code
    Code:
    std::string line;
    getline(cin,line);
    Here is an example. replace cin with whatever input stream you are using.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Well how would I implement that, in the code I have placed above?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    something like this:

    Code:
    while (!Istream.eof()) {
        getline(Istream,stringLine);
    	totalLines++;
       if (stringLine.find(userDefinedSearch)) > 0) 
       {
    	counter++; 
    	cout    << userDefinedSearch << endl;
    	Ostream << userDefinedSearch << endl;
    		
    	Ostream << counter << ") occurence is on line: " << line << "  " << userDefinedSearch << endl;
    	cout    << counter << ") occurence is on line: " << line << "  " << userDefinedSearch << endl << endl;	
    		 
       }
    }

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    All this seems to do is count every single line and imply that whatever is searched for is located on every line. And it reads through the entire document and prints.

    However it still is failing to print the actual line that the word is located on.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    think about it awhile, it will come to you. I'm not going to write your while program for you. All I posted was a brief description of how to use getline() and check if the line contains the search string. If you want to see the whole line, just use cout to display it on the screen.
    Code:
    cout << stringLine << endl;

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Ok, well thank you.


    The only thing that still baffles me is that it is reading in every line in the txt document. Just placing whatever word searched.

    Thanks for the help though

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what's what that while statement means -- read every line in the text document until end-of-file is reached. You program needs to do that in order to determine line numbers etc. where the search string occurs.

    Please don't stop asking questions -- you need to make some effort to solve your program, then post code for more information.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> while (!Istream.eof())

    Ancient Dragon, you should not be using (or advising the use) of eof() in this way. It may cause the loop to be executed one extra time at the end. Use the stream operation as the loop control: while (getline(Istream,stringLine)).

    The return value of getline or operator>> evaluates to true if the read is successful, and false if it is not (the stream is returned which is implicitly converted to void* that is null if the stream is in a fail state). Since you want the body of your loop to execute while the read is successful, it is the best solution.

    The last call to getline will probably discard the final newline in the data file, but if you use operator>>, any trailing whitespace, including a trailing newline will still be in the file stream after what should be the last time through the loop. If you use eof() at that point, it will return false (since there is one more newline in the file) and the loop will be run again. The read will fail since there is no actual data after the newline, but most code will just process the previous data again. So it's generally better to consistently use the technique that works better in many cases and the same in the rest.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    According to this the extraction operator >> and getline() always returns a reference to the ifstream object. That's why I was confused about its use. But a short example test program shows that you're right (as you obviously already know )

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ancient Dragon, you should not be using (or advising the use) of eof() in this way. It may cause the loop to be executed one extra time at the end. Use the stream operation as the loop control: while (getline(Istream,stringLine)).
    ...and it can cause an infinite loop. There are stream errors that can occur while reading from a file, and if a stream error occurs it will prevent reading any more data from the file, and since eof won't have been encountered, the loop will loop indefinitely.

    According to this the extraction operator >> and getline() always returns a reference to the ifstream object.
    An ifstream object will evaluate to false if certain error flags are set, which occurs when you are unable to read from the stream for any reason. Reading from a file after eof has been encountered causes an ifstream object to evaluate to false as well as other stream errors.

    Here are the details:
    http://www.ufaqs.com/usenet/new/usen...t_5_of_10).htm

    [15.3] How does that funky while (cin >> foo) syntax work?

    See the previous FAQ[15.2] for an example of the "funky while (cin >> foo) syntax."

    The expression (cin >> foo) calls the appropriate operator>> (for example, it calls the operator>> that takes an istream on the left and, if foo is of type int, an int& on the right). The istream operator>> functions return their left argument by convention, which in this case means it will return cin. Next the compiler notices that the returned istream is in a boolean context, so it converts that istream into a boolean.

    To convert an istream into a boolean, the compiler calls a member function called istream:: operator void*(). This returns a void* pointer, which is in turn converted to a boolean (NULL becomes false, any other pointer becomes true). So in this case the compiler generates a call to cin.operator void*(), just as if you had casted it explicitly such as (void*)cin.

    The operator void*() cast operator returns some non-NULL pointer if the stream is in a good state, or NULL if it's in a failed state. For example, if you read one too many times (e.g., if you're already at end-of-file), or if the actual info on the input stream isn't valid for the type of foo (e.g., if foo is an int and the data is an 'x' character), the stream will go into a failed state and the cast operator will return NULL.

    The reason operator>> doesn't simply return a bool (or void*) indicating whether it succeeded or failed is to support the "cascading" syntax:

    cin >> foo >> bar;
    Last edited by 7stud; 11-28-2005 at 06:45 PM.

  14. #14
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Quoted by Ancient D

    Getting the exact position in the file where the string starts will prove to be more problematic. One way is to read the entire file into memory so that you can easily calculate the offset of the string from the beginning of the input buffer.
    Ouch... . Reading an entire file into memory can not be advised regardless of how small the file is. In fact very few programs warrant the use of such action, with a few exceptions, such as word search programs etc.

    I've not read the entire thread but surely the OP could achieve his objectives by reading one line at a time into the buffer? On the other hand you may have already realised this.


  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by treenef
    Ouch... . Reading an entire file into memory can not be advised regardless of how small the file is. In fact very few programs warrant the use of such action, with a few exceptions, such as word search programs etc.
    You must be living with 30 year-old hardware. Several meg files is nothing nowdays to read into memory at once. Of course reading really huge files (1 Gig or more) all into memory will be nearly impossible, but there are other techniques used to deal with them which is probably way behond the requirements of this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM