逐行读取文件的问题

Mau*_*rus 1 c++ file-io fstream

我正在尝试完成练习以编写一个程序,该程序接受以下命令行参数:输入文件,输出文件和未指定数量的单词.程序是逐行读取输入文件的内容,找到给定哪个行包含该单词的每个单词,并将带有行号的行打印到输出文件.这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试运行它,我得到给定的第一个单词的预测输出,但是找不到它后面的任何单词.例如,对于上面的源代码,将提供以下输出:

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:
Run Code Online (Sandbox Code Playgroud)

也就是说,它会找到所给出的第一个单词的所有实例,但不会发现任何后续内容.我在这做错了什么?

编辑:我一直试图想办法回到文件的开头,但是在找不到一个叫做"rewind()"之类的方法之后,我放弃了.我in.seekg(0, ios::beg)在while循环后添加,它仍然给我相同的错误输出.

编辑2:好的,我终于放弃并意识到如果没有使用对的向量的某种野性尝试将无法得到我原来想要的输出,所以我放弃并决定以这种形式打印:

found in at line #31:         cerr << "Error #2: input file could not be opened\n";
found out at line #34:     ofstream out(argv[2]);
Run Code Online (Sandbox Code Playgroud)

也就是说,它按顺序打印所有行,并使用找到的特定单词将每个行打印出来.这是while循环:

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;
}
Run Code Online (Sandbox Code Playgroud)

无论如何,感谢所有的帮助!练习本身并没有说输出必须以任何方式进行格式化,因此我认为它已完全完成.

ken*_*ytm 5

i=3,内部while(getline())循环中将文件读取到EOF .因此,i=4,5,6…该文件不再可供阅读.

您可能需要倒带输入文件回到开头内循环结束后,或切换循环顺序(while(getline(...)) { ... for (int i = 3; ...) { ... } ... })