C++ 从文件中删除特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26576714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:38:24  来源:igfitidea点击:

Deleting specific line from file

c++

提问by Venraey

These are the contents of my example file:

这些是我的示例文件的内容:

abcdefg hijk lmnopqrstAB CSTAKLJSKDJD KSA FIND ME akjsdkjhwjkjhasfkajbsdh ADHKJAHSKDJH

abcdefg hijk lmnopqrstAB CSTAKLJSKDJD KSA FIND ME akjsdkjhwjkjhasfkajbsdh ADHKJAHSKDJH

I need to find and delete the 'FIND ME' inside of the file so the output would look like this:

我需要找到并删除文件中的“FIND ME”,这样输出将如下所示:

abcdefg hijk lmnopqrstAB CSTAKLJSKDJD KSA akjsdkjhwjkjhasfkajbsdh ADHKJAHSKDJH

abcdefg hijk lmnopqrstAB CSTAKLJSKDJD KSA akjsdkjhwjkjhasfkajbsdh ADHKJAHSKDJH

I have tried the following method of doing getline and then writing all of the contents except the FIND ME into a temporary file and then rename the temporary file back.

我尝试了以下方法来执行 getline,然后将除 FIND ME 之外的所有内容写入临时文件,然后将临时文件重命名回。

string deleteline;
string line;

ifstream fin;
fin.open("example.txt");
ofstream temp;
temp.open("temp.txt");
cout << "Which line do you want to remove? ";
cin >> deleteline;



while (getline(fin,line))
{
    if (line != deleteline)
    {
    temp << line << endl;
    }
}

temp.close();
fin.close();
remove("example.txt");
rename("temp.txt","example.txt");

but it doesn't work. Just as a side note: the file has NO newline/linefeeds. So the file contents are all written in 1 line.

但它不起作用。正如旁注:该文件没有换行符/换行符。所以文件内容都写在1行。

EDIT:

编辑:

FIXED CODE:

固定代码:

while (getline(fin,line))
{
    line.replace(line.find(deleteline),deleteline.length(),"");
    temp << line << endl;

}

This gets me the results I expected. Thank you everyone for helping!

这让我得到了我预期的结果。谢谢大家的帮助!

采纳答案by gmas80

Try this:

尝试这个:

line.replace(line.find(deleteline),deleteline.length(),"");

回答by Cuinn Herrick

In case anyone would like it I have converted Venraey's useful code to a function:

如果有人愿意,我已将 Venraey 的有用代码转换为函数:

#include <iostream>
#include <fstream>

void eraseFileLine(std::string path, std::string eraseLine) {
std::string line;
std::ifstream fin;

fin.open(path);
std::ofstream temp; // contents of path must be copied to a temp file then renamed back to the path file
temp.open("temp.txt");

while (getline(fin, line)) {
    if (line != eraseLine) // write all lines to temp other than the line marked fro erasing
        temp << line << std::endl;
}

temp.close();
fin.close();

const char * p = path.c_str(); // required conversion for remove and rename functions
remove(p);
rename("temp.txt", p);}

回答by X1NK3R

I'd like to clarify something. Although the answer provided by gmas80 could work, for me, it didn't. I had to modify it somewhat, and here's what I ended up with:

我想澄清一些事情。尽管 gmas80 提供的答案可以工作,但对我来说却没有。我不得不稍微修改一下,这就是我最终得到的结果:

position = line.find(deleteLine);

if (position != string::npos) {
    line.replace(line.find(deleteLine), deleteLine.length(), "");
}

Another thing that didn't satisfy me was that it left blank lines in the code. So I wrote another thing to delete the blank lines:

另一件让我不满意的事情是它在代码中留下了空行。所以我写了另一件事来删除空行:

if (!line.empty()) {
    temp << line << endl;
}