fstream counting new lines

i need help counting line numbers as i read through a file. the goal is to keep track of what words appear on what line number and store the word and line number into a Binary Tree. My problem though is correctly tracking the line number as i get different results. i didnt include it in the snipet but tmp is just a string and current line number is set to 1. the line numbers are ok if there is only one word in that line but if there are more than one words then only the last word on that line has the correct line number. thanks to anybody who can help me out here.
Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cout<<"Please provide a filename: ";
	cin>>userFile;
	fstream doc(userFile.c_str());
	if (doc)
		{
		while (!doc.eof())
			{
			//ss<<current_line_num;
			doc>>tmp;
			tmp.erase (remove (tmp.begin(), tmp.end(), ' '), tmp.end());
            tmp.erase (remove (tmp.begin(), tmp.end(), ','), tmp.end());
            tmp.erase (remove (tmp.begin(), tmp.end(), '.'), tmp.end());
            tmp.erase (remove (tmp.begin(), tmp.end(), '\''), tmp.end());
			b.insert(b.getRoot(),tmp);
			if (doc.peek() =='\n')
					{
					++current_line_num;
					}
			cout<<current_line_num<<" "<<tmp<<" ";
			}
		doc.close();
		doc.clear();
		break;
		}
	else
		cerr<<"File Not Found!"<<endl;
To read a complete line, use std::getline()
http://www.cplusplus.com/reference/string/getline/

Increment current_line_num after each getline()
but get line would store an entire sentence where as i want to store only individual words into the tree.
Read a complete line, and then process each individual word in that line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string line ;
int current_line_num = 0 ;
while( std::getline( doc, line ) ) // for each line in file
{
    // spit the line into white space delimited tokens
    std::istringstream stm(line) ;
    std::string word ;
    while( stm >> word ) // for each word in the line
    {
        // do whatever with the word
    }

    ++ current_line_num ;  // move on to the next line
}

it works but the first word of a line is cutt off there are more than one word.
apple
apple cats
apple cats dogs


the word apple in line 2 and 3 wont show up, cats and dogs do with the correct line number thanks JLB for your help so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cout<<"Please provide a filename: ";
	cin>>userFile;
	fstream doc(userFile.c_str());
	if (doc)
		{
		while (!doc.eof())
			{
			while (getline(doc,tmp))
				{
					istringstream stm(tmp);
					string word;
					while (stm>>word)
						{
						stm>>word;
						tmp.erase (remove (tmp.begin(), tmp.end(), ' '), tmp.end());
    		        	tmp.erase (remove (tmp.begin(), tmp.end(), ','), tmp.end());
    		        	tmp.erase (remove (tmp.begin(), tmp.end(), '.'), tmp.end());
    		        	tmp.erase (remove (tmp.begin(), tmp.end(), '\''), tmp.end());
						b.insert(b.getRoot(),tmp);
						cout<<"lin: "<<current_line_num<<" word: "<<word<<" ";
						}
				++ current_line_num;
				}
			}
		doc.close();
		doc.clear();
		break;
		}
	else
		cerr<<"File Not Found!"<<endl;
See if you can make sense of these modifications:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if (doc)
{
    // while (!doc.eof())
    //{
    while (getline(doc,tmp))
    {
        istringstream stm(tmp);
        string word;
        while (stm>>word)
        {
            //stm>>word; // ignore the word already read ??? 
            //tmp.erase (remove (tmp.begin(), tmp.end(), ' '), tmp.end());
            
            //tmp.erase (remove (tmp.begin(), tmp.end(), ','), tmp.end());
            word.erase (remove (word.begin(), word.end(), ','), word.end());
            
            //tmp.erase (remove (tmp.begin(), tmp.end(), '.'), tmp.end());
            word.erase (remove (word.begin(), word.end(), '.'), word.end());
            
            //tmp.erase (remove (tmp.begin(), tmp.end(), '\''), tmp.end());
            word.erase (remove (word.begin(), word.end(), '\''), word.end());
            
            //b.insert(b.getRoot(),tmp);
            if( !word.empty() ) b.insert(b.getRoot(),word);
            
            cout<<"lin: "<<current_line_num<<" word: "<<word<<" ";
        }
        ++ current_line_num;
    }
    //}
//doc.close();
//doc.clear();
//break;
}
Topic archived. No new replies allowed.