0

I want to analyze the content of a text file & search for a string in the file.
It is assumed a sentence is terminated by a , . ; ? !

This is what i have so far

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;
    char filename[30];
    char buffer[255];
    size_t found;
    string s;
    string str;
    size_t found;
    
    cout << "Enter filename (including .txt): ";
    cin >> filename;
    cout <<"Word to search for: ";
    cin >> buffer;

    fin.open(filename);
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

	while(!fin.eof())
	{
        found=str.find_first_of(".,?;!");
        // use getline to read entire line 
  
       getline(fin, s);
        cout << found << endl;
        
    }
    
    fin.close();    
    system("pause");
    return 0;
}

Can someone help me please

4
Contributors
3
Replies
13
Views
8 Years
Discussion Span
Last Post by mvmalderen
0

that loop is backwards

while( getline( fin, s) )
{
    found=str.find_first_of(".,?;!");
   // That might find a sentence, but does not mean its the entire
   // sentence -- part of the sentence may have been on the previous
   // line -- like this comment.
}
0

while(!fin.eof()) , before testing for EOF you should have performed at least one read operation (read this), so this would be a correct way to do it:

[B]getline(fin, s);[/B]
while(!fin.eof())
{
  getline(fin, s);
  cout << found << endl;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.