I'm trying to complete an exercise to write a program that takes the following command line arguments: an input file, an output file, and an unspecified number of words. The program is to read the contents of the input file line by line, find for each word given which lines contain the word, and print the lines with their line number to the output file. Here's my code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 4) {
cerr << "Error #1: not enough arguments provided\n";
return 1;
}
ifstream in(argv[1]);
if (!in.is_open()) {
cerr << "Error #2: input file could not be opened\n";
return 2;
}
ofstream out(argv[2]);
if (!out.is_open()) {
cerr << "Error #3: output file could not be opened\n";
return 3;
}
ostringstream oss;
for (int i = 3; i < argc; ++i) {
int k = 0;
string temp;
oss << argv[i] << ":\n\n";
while (getline(in, temp)) {
++k;
unsigned x = temp.find(argv[i]);
if (x != string::npos)
oss << "Line #" << k << ": " << temp << endl;
}
}
string copy = oss.str();
out << copy;
in.close();
out.close();
return 0;
}
If I try to run that, I get the predicted output for the first word given, but any words following it aren't found. For example, for the source code above will give the following output:
in:
Line #1: #include <iostream>
Line #2: #include <fstream>
Line #3: #include <string>
Line #4: #include <sstream>
Line #5: using namespace std;
Line #7: int main(int argc, char* argv[]) {
Line #12: ifstream in(argv[1]);
Line #13: if (!in.is_open()) {
Line #14: cerr << "Error #2: input file could not be opened\n";
Line #22: ostringstream oss;
Line #23: string temp;
Line #24: for (int i = 3; i < argc; ++i) {
Line #26: int k = 0;
Line #28: while (getline(in, temp)) {
Line #30: unsigned x = temp.find(argv[i]);
Line #31: if (x != string::npos)
Line #32: oss << "Line #" << k << ": " << temp << endl;
Line #35: string copy = oss.str();
Line #37: in.close();
out:
That is, it'll find all the instances of the first word given but not any following. What am I doing wrong here?
EDIT: I had been trying to figure out a way to go back to the beginning of the file, but I sorta gave up after I couldn't find a method called "rewind()" or something like that. I added in.seekg(0, ios::beg)
after the while loop, and it still gives me the same wrong output.
EDIT 2: Okay, I finally gave in and realized I wouldn't be able to get my original desired output without some sort of wild attempt at using vectors of pairs, so I gave in and decided to make it print in this form:
found in at line #31: cerr << "Error #2: input file could not be opened\n";
found out at line #34: ofstream out(argv[2]);
That is, it prints all the lines in order and heads each one with the specific word found. Here's the while-loop:
ostringstream oss;
string temp;
while(getline(in,temp)) {
static int count = 1;
for (int i = 3; i < argc; ++i) {
unsigned foundWord = temp.find(argv[i]);
if (foundWord != string::npos)
oss << "found " << argv[i] << " at line #" << count << ": " << temp << endl;
}
++count;
}
Anyway, thanks for all the help! The exercise itself didn't say the output had to be formatted in any way, so I consider it fully completed.