Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;删除.txt文件中的所有行,直到达到某个关键字为止 我对C++是相当新的,我正在尝试编写一个代码来运行对大数据文件的分析。我已经成功地编写了生成一个文本文件的代码,其中每行仅显示一个单词/数字(有数百万行)。然而,前约3000行包含了我分析中不需要的无用内容_C++_Text - Fatal编程技术网

C++;删除.txt文件中的所有行,直到达到某个关键字为止 我对C++是相当新的,我正在尝试编写一个代码来运行对大数据文件的分析。我已经成功地编写了生成一个文本文件的代码,其中每行仅显示一个单词/数字(有数百万行)。然而,前约3000行包含了我分析中不需要的无用内容

C++;删除.txt文件中的所有行,直到达到某个关键字为止 我对C++是相当新的,我正在尝试编写一个代码来运行对大数据文件的分析。我已经成功地编写了生成一个文本文件的代码,其中每行仅显示一个单词/数字(有数百万行)。然而,前约3000行包含了我分析中不需要的无用内容,c++,text,C++,Text,唯一的问题是,根据输入文件的不同,实际数据从不同的行号开始 有没有办法编写一个快速代码来搜索文本文档并删除所有行,直到找到关键字“” 更新: 我成功了!可能比建议的要复杂一点,但它仍然有效 谢谢你的帮助 #include <iostream> #include <fstream> #include <cstdio> #include <cstring> using namespace std; in

唯一的问题是,根据输入文件的不同,实际数据从不同的行号开始

有没有办法编写一个快速代码来搜索文本文档并删除所有行,直到找到关键字
“”

更新: 我成功了!可能比建议的要复杂一点,但它仍然有效

谢谢你的帮助

    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <cstring>
    using namespace std;

    int main()
    {

        int counter = 0;

    ifstream FileSearch("OutputVector.txt"); // search OutputVector input file.

    while(!FileSearch.eof())
    {
        counter++;
        string temp;
        FileSearch >> temp;

        if(temp == "<event>")
        {
            break; //While loop adding +1 to counter each time <event> is not found.
        }
    }



    std::ofstream outFile("./final.txt"); //Create output file "final.txt."
    std::string line;

 std::ifstream inFile("OutputVector.txt"); //open input file OutputVector again.

 int count = 0;

 while(getline(inFile, line)){

     if(count > counter-2){
        outFile << line << std::endl;
     }
     count++; //while loop counts from counter-2 until the end and writes them to the new file.
 }
 outFile.close();
 inFile.close(); //close the files.
 remove("OutputVector.txt"); //Delete uneeded OutputVector File.
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int计数器=0;
ifstream FileSearch(“OutputVector.txt”);//搜索OutputVector输入文件。
而(!FileSearch.eof())
{
计数器++;
字符串温度;
文件搜索>>温度;
如果(温度==“”)
{
break;//While未找到每次向计数器添加+1的循环。
}
}
std::ofstreamoutfile(“./final.txt”);//创建输出文件“final.txt”
std::字符串行;
std::ifstream infle(“OutputVector.txt”);//再次打开输入文件OutputVector。
整数计数=0;
while(getline(infle,line)){
如果(计数>计数器2){
外文件基本骨架:

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line))
{
    // optional: increment line number here
    if (line.find("<event>") != line.npos)
    {  // Deity of choice help you if <event> naturally occurs in junk lines. 
       // Extra smarts may be required here.
        doStuffWithRestOfFile(stream);
        break;
    } 
}
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(流,行))
{
//可选:此处增加行号
if(line.find(“”)=line.npos)
{//如果选择的神自然出现在垃圾堆中,它会帮助你。
//这里可能需要额外的智能。
dostufwithrestoffile(流);
打破
} 
}
没有足够的信息说明您希望如何修改源文件以回答该子问题。一旦您让读者开始阅读,如果您还没有弄明白,请提出一个新问题

编辑:短版

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line) && (line.find("<event>") == line.npos))
{
    // optional: increment line number here
}
doStuffWithRestOfFile(stream);
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(stream,line)和&(line.find(“”==line.npos))
{
//可选:此处增加行号
}
dostufwithrestoffile(流);
基本框架:

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line))
{
    // optional: increment line number here
    if (line.find("<event>") != line.npos)
    {  // Deity of choice help you if <event> naturally occurs in junk lines. 
       // Extra smarts may be required here.
        doStuffWithRestOfFile(stream);
        break;
    } 
}
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(流,行))
{
//可选:此处增加行号
if(line.find(“”)=line.npos)
{//如果选择的神自然出现在垃圾堆中,它会帮助你。
//这里可能需要额外的智能。
dostufwithrestoffile(流);
打破
} 
}
没有足够的信息说明您希望如何修改源文件以回答该子问题。一旦您让读者开始阅读,如果您还没有弄明白,请提出一个新问题

编辑:短版

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line) && (line.find("<event>") == line.npos))
{
    // optional: increment line number here
}
doStuffWithRestOfFile(stream);
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(stream,line)和&(line.find(“”==line.npos))
{
//可选:此处增加行号
}
dostufwithrestoffile(流);
基本框架:

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line))
{
    // optional: increment line number here
    if (line.find("<event>") != line.npos)
    {  // Deity of choice help you if <event> naturally occurs in junk lines. 
       // Extra smarts may be required here.
        doStuffWithRestOfFile(stream);
        break;
    } 
}
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(流,行))
{
//可选:此处增加行号
if(line.find(“”)=line.npos)
{//如果选择的神自然出现在垃圾堆中,它会帮助你。
//这里可能需要额外的智能。
dostufwithrestoffile(流);
打破
} 
}
没有足够的信息说明您希望如何修改源文件以回答该子问题。一旦您让读者开始阅读,如果您还没有弄明白,请提出一个新问题

编辑:短版

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line) && (line.find("<event>") == line.npos))
{
    // optional: increment line number here
}
doStuffWithRestOfFile(stream);
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(stream,line)和&(line.find(“”==line.npos))
{
//可选:此处增加行号
}
dostufwithrestoffile(流);
基本框架:

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line))
{
    // optional: increment line number here
    if (line.find("<event>") != line.npos)
    {  // Deity of choice help you if <event> naturally occurs in junk lines. 
       // Extra smarts may be required here.
        doStuffWithRestOfFile(stream);
        break;
    } 
}
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(流,行))
{
//可选:此处增加行号
if(line.find(“”)=line.npos)
{//如果选择的神自然出现在垃圾堆中,它会帮助你。
//这里可能需要额外的智能。
dostufwithrestoffile(流);
打破
} 
}
没有足够的信息说明您希望如何修改源文件以回答该子问题。一旦您让读者开始阅读,如果您还没有弄明白,请提出一个新问题

编辑:短版

std::ifstream stream("file name goes here")
std::string line;
// optional: define line number here
while (std::getline (stream, line) && (line.find("<event>") == line.npos))
{
    // optional: increment line number here
}
doStuffWithRestOfFile(stream);
std::ifstream流(“文件名在这里”)
std::字符串行;
//可选:在此处定义行号
while(std::getline(stream,line)和&(line.find(“”==line.npos))
{
//可选:此处增加行号
}
dostufwithrestoffile(流);

如果要使用新版本(无开头)覆盖文件,可以将所有文件读取到内存并覆盖,或者在读取第一个文件时写入第二个文件,然后移动/重命名

要读取所有行直到找到

std::ifstream input_file( filePath );
std::string line;
int current_line = 0;

do
{
   std::getline( input_file, line );
   ++current_line;
}
while( line.find("<event>") == line.npos );
// use input_line to process the rest of the file
std::ifstream输入文件(filePath);
std::字符串行;
int电流_线=0;
做
{
std::getline(输入文件,行);
++当前_线;
}
while(line.find(“”==line.npos);
//使用input_行处理文件的其余部分

请记住,如果
是第一行,那么在
do while
之后,
当前行将包含1,而不是0

,如果您想用新版本(没有开头)覆盖文件,您可以将所有文件读取到内存并覆盖它,或者在读取第一行时写入第二个文件