search function fstream

Hi guys, as you can see in the code section I wrote a program which searchs a word in a file and then gives out the line in which the word was found. The user is able to input an amount of lines which should been given out before the found line. For example the Text is:

Hello hello hello
hello hello hello
test example example

So i want to change the code that for example the user puts in 2 and all 3 lines are given out. How can i implement that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
         else {
            fstream myfile(argv[2]);
            if (myfile.is_open()) {
          size_t pos;
                
                while (getline(myfile, line)) {
                    pos = line.find(argv[1]);
                    if (pos != string::npos) {

                        cout << line;
                        cout << "\n";
                    }

                }

            }



        }
What do you mean by "user puts in 2 and all 3 lines are given out"?
Last edited on
So the program searches for a word in the text. all lines with the word in it are given out. I want that a user can type in 2 wordtosearch for example. Then the line in which the word was found should be given out and the 2 lines before that line even if they dont have the word in it. And if he puts in 10 there should be the line with the word and 10 lines before that
Hello NMI21,

It is usually a good practice to post enough of a complete program that can be compiled and run.

In the if statement checking "is_open()" may work, but just because the file stream is open may not mean it is usable. if (!myfile). A better choice would be to use the if statement to check the file stream and if something is wrong print an error message and leave the program. After you can continue the program without having it wrapped in an if statement.

Without enough code to test the program what you have posted looks like it should work.

Over time I have come up with this:
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "" };  // <--- Put File name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
    //std::cout << "\n     File " << std::quoted(inFileName) << " did not open.\n";  // <--- Requires header file "<iomanip>".
    std::cout << "\n     File \"" << inFileName << "\" did not open.\n";

    return 1;
}

It may be more than you need, but it helps when learning to use file streams.

Andy
Hi any thats only a small part from miy program! I added the other stuff already. I already understand the fstream and stuff! Thanks anyways
So the program searches for a word in the text. all lines with the word in it are given out. I want that a user can type in 2 wordtosearch for example. Then the line in which the word was found should be given out and the 2 lines before that line even if they dont have the word in it. And if he puts in 10 there should be the line with the word and 10 lines before that


As a starter, perhaps:

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

int main(int argc, char* argv[])
{
	// NOTE NO CHECK FOR VALID COMMAND LINE ARGUMENTS

	// argv[1] - text to find
	std::fstream myfile(argv[2]);					// File to read
	const std::size_t toshow {static_cast<size_t>(atoi(argv[3]))};	// Number of lines to show backwards

	if (myfile.is_open()) {
		std::vector<std::string> body;

		for (std::string line; std::getline(myfile, line); body.push_back(std::move(line)));

		for (size_t i = 0; i < body.size(); ++i)
			if (body[i].find(argv[1]) != std::string::npos) {
				for (size_t d = i - std::min(i, toshow); d < i; ++d)
					std::cout << '*' << body[d] << '\n';

				std::cout << body[i] << "\n\n";
			}
	}
}

Last edited on
Topic archived. No new replies allowed.