Find line number of a word in a txt file

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
int counter=0;
string word;
        if (argc!=2)
        {
        cerr<<"IMPROPER USAGE!! CORRECT USAGE IS:\n"<<argv[0]<<"FILE.TXT"<<endl;
        }
        else
        {
        cout<<"find what word?:";
        cin>>word;
        ifstream wordfile(argv[1]);

        while (!wordfile.eof())
                {
                counter++;
                string temp;
                getline (wordfile,temp);
        //      wordfile >>temp;
                if (temp == word)
                        {
                        cout<<word<<" found on "<<counter<<endl;
                        }
                }

        }
return 0;
}

if for example my txt file contains sentences and not just 1 word per line this code does not work and i'm not sure how to fix it.(only the words hello and work would show up and the program would just end for all the other words)
1
2
3
4
hello
this code
does not
work
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>  // include this to format your output so it is more readable
using namespace std;

int main(int argc, char *argv[])
{
int counter = 0;
unsigned int pos;
bool wordFound;
string fileName,
	word,
	templine,
	newLine;

        /*if (argc!=2)
        {
			cerr<<"IMPROPER USAGE!! CORRECT USAGE IS:\n"<<argv[0]<<"FILE.TXT"<<endl;
        }*/

		if (argc > 1)
			fileName = argv[1];
        else
        {
			cout << "What is the name of the file? ";
			getline(cin, fileName);

			//cout<<"find what word?:";
			//cin>>word;
			
		}

        ifstream wordfile(fileName.data()); // open up the file you declared.

		// Test to see if the file is opened and
		// exit program if not.
		if (!wordfile)
		{
			perror(fileName.data());
			exit(1);
		}

		// get the word that you wish to search for
		cout << " which word would you like to search for ";
		getline(cin, word);

        //while (!wordfile.eof())

		// test is true as long as a line is being read in.
		while(getline(wordfile, templine))
        {
			pos = 0;			// keeps track of position of search index within the file.
			wordFound = true;	// bool to let you know when a word is found
            counter++;			// this is the counter to determin when your start a new line.	

			newLine = templine; // asigns the contents of the templine to a newLine this will 
								// later sever as a the new starting point everytime a word is 
								// found.


            /*string temp;
            getline (wordfile,temp);
			wordfile >>temp;*/

			// search through the contents of each line and when the searched word is found 
			// out put the line it is on, repostiont the starting index where the current word 
			// has been found the continue search.
			while ((pos = newLine.find(word, pos)) < templine.npos)
			{
				if (wordFound)
				{
					// print the line number and the line containing the searched word
					// this is line has been formated.
					cout << right << setw(5) << counter << ": " << left << templine << endl;
					wordFound = false;
				}
				newLine = newLine.substr(pos + (word.length()));
				pos = 0;
			}


            /*if (temp == word)
            {
				cout<<word<<" found on "<<counter<<endl;
            }*/
		}

return 0;
}


I hope this code helps... I commented out most of your code and replaced it with the only way i know how to make this work... I've made extensive comments with in the code to help guide you along my thought processes... Good luck coding!
Topic archived. No new replies allowed.