Question

Hello, I'm having trouble solving this problem. I have tried some other codes and they seem like they work but I am not sure how the files are made or how this code works. It will be helpful that you show me how. Thank you so much. (In C++)

Write a program find that searches all files specified on the command line and prints out all lines containing a keyword. For

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>
#include <fstream>
using namespace std;
void findKeyWord(string fileName, string key)
{
    ifstream fin;
    fin.open(fileName.c_str());
    if (!fin.is_open())
        cout << "Unable to open the input file." << endl;
    else
    {
        string line;
        while (!fin.eof())
        {
            getline(fin, line);
            if (line.find(key) != -1)
                cout << fileName << ": " << line << endl;
        }

        fin.close();
    }
}
int main(int argc, char **argv)
{
    if (argc < 3)
    {
        cout << "Invalid number of parameters." << endl;
        return 0;
    }
    for (int i = 2; i < argc; i++)
        findKeyWord(argv[i], argv[1]);
}

Add a comment
Know the answer?
Add Answer to:
Hello, I'm having trouble solving this problem. I have tried some other codes and they seem like they work but I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I...

    Hello, I am having some trouble with a supermarket checkout simulation program in C++. What I have so far just basically adds customers to the queue and prints when they arrive. I am struggling with how to implement a way of keeping track of when a given customer finishes(I will attach what I have so far). I had already created queue and node classes (with headers and cpp files) that I modified in my attempt. I would be very grateful...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT