Finding what line a word is on

Okay, so here's the deal. I made a program that loops through a text file. Now all I need is when if finds that word is to print the line number that the word was first found on, or 0 if it isn't found.

I'm pretty dang confused on this as of now and I don't have a lead. Help is appreciated. :)
Nevermind I seem to have solved my own problem. here is the full code:

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

int main()
{
    char FileName[256];
    char SearchWord[256];
    int counter = 0;

    cout << "Enter the word you want to search for: ";
    cin >> SearchWord;
    cout << "\n\n";
    cout << "Enter the name of file you want to open: ";
    cin >> FileName;
    ifstream FileSearch(FileName);

    while(!FileSearch.eof())
    {
        counter++;
        string temp;
        FileSearch >> temp;

        if(temp == SearchWord)
        {
            cout << SearchWord << " found on line: " << counter << "\n\n";
            break;
        }
    }
    return 0;
}
See this reference page: http://www.cplusplus.com/reference/string/operator%3E%3E/

The extraction operator will only read a single word (white space delimited character sequence) from the stream. That is, the line counting scheme you employ does not count lines, but words. You could instead use the getline function for strings to extract each line in a string. Then you can search the extracted string for occurrence of the word in some inner loop. The outer loop will count the lines.

Also see: http://www.cplusplus.com/reference/string/getline/

Regards
Last edited on
Thanks, having a new problem.. when I call a file in my program:

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


int main(int argc, char** argv)
{
    char FileName[256];
    char SearchWord[256];
    int counter = 0;

    cout << "Enter the word you want to search for: ";
    cin >> SearchWord;
    cout << "\n\n";
    cout << "Enter the name of file you want to open: ";
    cin >> FileName;
    cout << "\n\n";
    ifstream FileSearch;

    FileSearch.open(FileName);
    if(!FileSearch)
    {
        cout << "File was unable to be opened.\n";
    }
    else{
        while(!FileSearch.eof())
        {
            counter++;
            string temp;
            FileSearch >> temp;

            if(temp == SearchWord)
            {
                cout << SearchWord << " found on line: " << counter << "\n\n";
                break;
            }
            if(temp != SearchWord)
            {
                if(FileSearch.eof())
                {
                    cout << 0;
                }
            }
        }
    }
    return 0;
}


It will open a file, lets say you enter "Students" it will open a file named Students. If you try "Students.txt", and you have a a text file named "Students.txt" in the same directory, it won't open. What have I done wrong?
And @ simeonz, I'm expecting that in the text file every word will have it's own line. But thanks for future reference. :)
From which directory do you launch your app. Your working directory (initially the one in which you launch the executable) does not have to be the directory where the executable resides.
I'm launching it in the same directory that the .cpp file is. they both are in the same spot. It will read the file if I take off the .txt extension.
I have no idea. The code that opens the file looks fine. Are you sure that the extension is not there when you think you have removed it? (I know this is "check the cables" troubleshooting, but still.) Some OSes (read Windows) hide the extensions of registered document types. (unless instructed otherwise)

Sorry for being unhelpful, but I am shooting at the dark.
closed account (zwA4jE8b)
try adding

#include <iomanip>
#include <string>

then get the filename using

string FileName
..............
cout << "Enter the name of file you want to open: ";
getline (cin, FileName);
..............
FileSearch.open(FileName.c_str());

that method works all the time for me

also, if you right click on resources you can add your data files that way and they will be conveniently available
Last edited on
closed account (zwA4jE8b)
simeonz is right, you should type the filename in exactly as it is named, also including the extension
Why don't you just count '\n' s?

Also I've run into something similar myself, what was happening is I had copied the original file elsewhere and my program was sniping (opening from a distance lol{it ended up my fault of course}) the file. What I would try is to make a completely new file and name it something completely random, just to make sure there aren't any duplicates, then give it a try. When you find out it works great with the new files, troubleshoot the old files.
Topic archived. No new replies allowed.