Read end of line while looping word by word in c++
<p>Am reading a file which contains list of sentences , I need to read each word and try to figure out this word in which line number ..
the file contains :</p>

<pre><code>I am for truth
no matter who tells it,
I am for justice,
no matter who it is for or against
Malcom X 
</code></pre>

<p>and I want the output to be in this form :</p>

<pre><code>against 4 
matter 4
am 1, 3 
no 2, 4
for 1, 3, 4 
or 4
I 1, 3 
tells 2
is 4 
truth 1
it 2, 4 
who 2,4
justice 3 
X 5
Malcolm 5
</code></pre>

<p>am using binary search trees and here is my code :</p>

<pre><code>int main(int argc, char *argv[]) {
    fstream infile ;
    BSTFCI &lt;string&gt;* bst = new BSTFCI&lt;string&gt;();
    string word;
    string line;
    infile.open("test.txt" , ios::in);
    if(infile.fail())
    {
      cout&lt;&lt;"Error Opening file"&lt;&lt;endl;
      return 0;
    }

    while(!infile.eof())
    {

        infile&gt;&gt;word;

        for(int i=0 ; i&lt;word.size();i++)
        {
            if(ispunct(word[i]))
            word.erase(i,1);        
        }
        cout&lt;&lt;count&lt;&lt;endl;
        if(!bst-&gt;search(word))
        {
          cout&lt;&lt;word&lt;&lt;endl;
          bst-&gt;insert(word);
          cout&lt;&lt;"add"&lt;&lt;endl;
        }
        else
        {
         cout&lt;&lt;word&lt;&lt;endl;
         cout&lt;&lt;"exist"&lt;&lt;endl;
        }
    }

    infile.close();
    return 0;
}
</code></pre>
