“Practice with command line arguments. You will need to use functionality from a
ID: 3912686 • Letter: #
Question
“Practice with command line arguments.You will need to use functionality from and
Other than the program's name there are two additional arguments on the command line:
a string to search for
the name of an existing text file (.txt or .cpp)
The program should print only those lines containing the search-string”
It's very much like when you search a word in the text book, the lines that have that key words show up as a list
“Practice with command line arguments.
You will need to use functionality from and
Other than the program's name there are two additional arguments on the command line:
a string to search for
the name of an existing text file (.txt or .cpp)
The program should print only those lines containing the search-string”
It's very much like when you search a word in the text book, the lines that have that key words show up as a list
“Practice with command line arguments.
You will need to use functionality from and
Other than the program's name there are two additional arguments on the command line:
a string to search for
the name of an existing text file (.txt or .cpp)
The program should print only those lines containing the search-string”
It's very much like when you search a word in the text book, the lines that have that key words show up as a list
Explanation / Answer
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
// taking command line arguments
// first one is filename, secod is word to search and third is filename
string tosearch = argv[1];
string fname = argv[2];
ifstream infile(fname);
// reading each line
string eachLine;
while (getline(infile, eachLine))
{
// if line has the word to search, printing that line
if (eachLine.find(tosearch) != std::string::npos) {
cout << eachLine << endl;
}
}
}
/*
SAMPLE INPUT: ./main Chegg text.txt
text.txt contains
Chegg is a good platform
India is an awesome country
There is Chegg company in India
That works pretty well
SAMPLE OUTPUT
Chegg is a good platform
There is Chegg company in India
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.