search for word in text file, display entire row once found

I've been trying to figure out to search for a word in a text file and then display everything in the same row as the word found int

ie this is whats in the file


john doe 3/21/1920 tech support review team 45,000


so user wants to find tech..and everything associated with it.

so program search for tech, when it does it then display the whole row.

john doe 3/21/1920 tech support review team 45,000

I can figure out how to search for a word, but no clue how to get it to then print out the row. This is all I can figure out to do.


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
ifstream FileSearch;
     FileSearch.open("employee");
     if(FileSearch.is_open())
     {string letters;// search word would be store here
     string row; ??stores entire row as string
     	while(1)
	 {
            cout<<"Enter search key  ";// get word to search for
	    getline(cin,letters);
            getline(FileRead, employee);
     	   if(FileRead.eof())
     		break;
     	
     		//search for word
	while((startP = employee.find(letters, startP))!=string::npos)
			
			   
      		cout<<row<<endl; //diplay entire row   		
     	} 	
     }
     else{
     	
     	cout<<"can not open a file 2"<<endl;
     }
In pseudocode, you want to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while true {
    Get the search key from the user
    if the key is blank then break
    findKeyInFile(search key);
}


void findKeyInFile(const string &key)
{
    open the file
    while (not at end of file) {
        read a line from the file
        if (the key is in the line just read) {
            output the line
        }
    }
    close the file
}
almost but no cigar..any suggestions..it compiles but doesn't actually search the word..or maybe it does, but doesn't give me expected results.. Not sure what is wrong..I just know its not giving me a result

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
void KeyWord(ifstream &FileSearch)
{ string letters;
   int position =-1;
   string line;
     ifstream readSearch;
     FileSearch.open("employee");
     if(FileSearch.is_open())
     { 
        
     cout<<"enter search word ";
     		cin>>letters;
			 "\n";
     	while(getline (FileSearch, line))
		 {	 position=line.find(letters,position+1);
     		if(position==string::npos)
     		break;
     		
     		cout<<line<<endl;
     	 }
     
     }
     else{
     	cout<<"Cant find"<<letters<<endl;
     }
}


This is the code example from my class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string quote = "If it walks like a duck, "
                   "and quacks like a duck, \n"
                   "then it just may be a duck - Reuther";

    int position = -1;
    while(1)
    {
        position = quote.find("duck", position+1);
        if(position == string::npos)
            break;

        cout<<position<<endl;
    }
    return 0;
}


Here's one way you could do it:

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
#include <iomanip>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdexcept>

struct LineInfo
{
    std::size_t lineNo;
    std::size_t pos;
    std::string line;
};

std::ostream& operator<<(std::ostream& os, const LineInfo& info);
std::vector<LineInfo> getLinesContaining(const std::string& filename, const std::string& key);

int main()
{
    std::string filename = "employee";
    std::string key = "tech";

    try {
        std::vector<LineInfo> lines = getLinesContaining(filename, key);

        std::cout << "Found " << lines.size() << " lines containing \"" << key << "\"\n\n";

        for (auto& l : lines)
            std::cout << l << '\n';
    }

    catch (std::exception& ex)
    {
        std::cerr << ex.what() << '\n';
    }
}

std::vector<LineInfo> getLinesContaining(const std::string& filename, const std::string& key)
{
    if (std::ifstream in = std::ifstream(filename))
    {
        std::vector<LineInfo> lines;

        std::string line;
        std::size_t linecount = 0;

        while (std::getline(in, line))
        {
            ++linecount;
            auto pos = line.find(key);
            if (pos != std::string::npos)
                lines.emplace_back(LineInfo{ linecount, pos, line });
        }

        return lines;
    }

    throw std::runtime_error("Unable to open file \"" + filename + '"');
}

std::ostream& operator<<(std::ostream& os, const LineInfo& info)
{
    os << std::setw(4) << info.lineNo << ": " << info.line << '\n';
    os << std::string(info.pos + 6, ' ') << "^\n";
    return os;
}
ok here is the working code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void KeyWord(ifstream &FileSearch) 
{
    string line;
    string letters;
    ifstream readSearch;
    
    cout<<"enter search word ";
     		cin>>letters;
			 "\n";
    FileSearch.open("employee");
     if(FileSearch.is_open())
    {
    
    while (getline(FileSearch, line)) {
        if (line.find(letters)!=string::npos) {
            cout << line << endl;
        }
    }
    cout << letters << " not found" << endl;
    }
}

Topic archived. No new replies allowed.