C++ 用C+重写grep+;

C++ 用C+重写grep+;,c++,vector,grep,C++,Vector,Grep,所以我正在尝试编写一个程序,它执行与grep相同的任务。最后,我想要一个包含四个参数的程序,第二个参数是要搜索的内容,第三个参数是输入文件,第四个参数是输出文件。我想我已经很好地掌握了如何去做,但像往常一样,理论很容易,而实际的编程我都搞糊涂了。基本上就是我现在所在的位置,我已经把文件放进去了,我试着搜索它,得到所有包含我要搜索的内容的行,以及那个数字的行 我想用一个向量来完成这个任务。我不完全确定该怎么做。将每一行单独地遍历并添加到向量中,然后遍历并选择其中包含所需字符串的行,并使用它们的数组

所以我正在尝试编写一个程序,它执行与grep相同的任务。最后,我想要一个包含四个参数的程序,第二个参数是要搜索的内容,第三个参数是输入文件,第四个参数是输出文件。我想我已经很好地掌握了如何去做,但像往常一样,理论很容易,而实际的编程我都搞糊涂了。基本上就是我现在所在的位置,我已经把文件放进去了,我试着搜索它,得到所有包含我要搜索的内容的行,以及那个数字的行

我想用一个向量来完成这个任务。我不完全确定该怎么做。将每一行单独地遍历并添加到向量中,然后遍历并选择其中包含所需字符串的行,并使用它们的数组位置作为行号,是否更容易?我想有一种方法可以只将包含所需字符串的行添加到向量中,但我不确定如何获得行号。我已经开始了几次,然后当我意识到一切都错了的时候,我抹掉了我所知道的

这就是我现在的处境:

#include <iostream>
#include <regex>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main (int argc, char* argv[]){

// validate the command line info
if( argc < 2 ) {
    cout << "Error: Incorrect number of command line arguments\n"
            "Usage: grep\n";
    return EXIT_FAILURE;
}

//Declare the arguments of the array
string query = argv[1]; 
string inputFileName = argv[2];
string outputFileName = argv [3];

// Validate that the file is there and open it
ifstream infile( inputFileName );
if( !infile ) {
    cout << "Error: failed to open <" << inputFileName << ">\n"
            "Check filename, path, or it doesn't exist.\n";
    return EXIT_FAILURE;
}

else{
vector<string> queries;


}

}

}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[]){
//验证命令行信息
如果(argc<2){

cout没有必要使用向量。在我看来,您应该使用std::getline一次遍历一行输入文件,尝试将每一行与正则表达式匹配,并立即输出成功的行。

实现完整grep的大部分复杂性将是处理几十个标志以修改其工作方式。a不尝试支持这些模式,只搜索一组文件以查找指定模式的简化版本可能非常简单。C++11版本大致如下所示:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <regex>

class line {
    std::string data;
public:
    operator std::string() const { return data; }

    friend std::istream &operator>>(std::istream &is, line &l) {
        return std::getline(is, l.data);
    }
};

void process(std::regex const &pattern, std::istream &file) {
    typedef std::istream_iterator<line> in;
    typedef std::ostream_iterator<std::string> out;

    std::copy_if(in(file), 
                in(), 
                out(std::cout, "\n"), 
                [&](std::string const &s) {return std::regex_search(s, pattern);});
}

int main(int argc, char **argv) { 
    if (argc < 2) {
        std::cerr << "Usage: grep <pattern> [file specification]";
        return 1;
    }

    std::regex pattern(argv[1], std::regex::nosubs | std::regex::optimize);

    if (argc < 3)
        process(pattern, std::cin);
    else
        for (int i=2; i<argc; ++i) {
            std::ifstream infile(argv[i]);
            std::cout << "\t-- " << argv[i] << " --\n";
            process(pattern, infile);
        }
    return 0;
}
#包括
#包括
#包括
#包括
#包括
班级线{
std::字符串数据;
公众:
运算符std::string()常量{返回数据;}
friend std::istream&operator>>(std::istream&is,line&l){
返回std::getline(is,l.data);
}
};
void进程(std::regex常量和模式,std::istream和文件){
typedef std::中的istream_迭代器;
typedef std::ostream_迭代器输出;
std::复制_if(文件中),
在()中,
out(std::cout,“\n”),
[&](std::string const&s){return std::regex_search(s,pattern);};
}
intmain(intargc,字符**argv){
如果(argc<2){

std::cerr难道你找不到更简单的方法,比如解决庞加莱猜想或黎曼假设吗?你真的看过正则表达式处理的代码吗?:-)Re:#包括grep的大部分复杂性都在上面。
grep
的原始作者也写了这部分。你写的只是一个微不足道的shell围绕regex API。更加努力。:)每个人都必须从某个地方开始。:)谢谢大家的帮助。我明白了,我对使用输出文件也很陌生,并计划今晚对它们进行一些阅读。使用这种方法将它们全部放进输出文件是否容易?另外,使用这种方法我如何保存要保留的数字行它们与我正在打印的行一致。只是一个循环,并使用迭代器作为行号?@Sh0gun是的,输出到文件没有什么不同,无论是从
向量执行,还是在匹配后直接输出每行。要获得行号,你将有一个计数器跟踪你正在处理的行号当一行匹配时,您只需使用该计数器的值作为行号。@Sh0gun另外,听起来您可能需要编写最终的程序。我建议您编写一个程序,将输入文件逐行回显到输出文件。然后修改它以包括行号。然后再次修改它,以仅输出符合要求的行适合某些条件,例如偶数行号。然后将条件更改为检查行是否与正则表达式匹配。非常感谢您的建议,我这样做了,而且更简单。