【发布时间】:2010-12-21 12:12:30
【问题描述】:
我正在尝试找到一种方法,用新行替换文件中包含字符串的行。
如果文件中不存在该字符串,则将其附加到文件中。
谁能给个示例代码?
编辑:如果我需要替换的行在文件末尾,是否还有?
【问题讨论】:
-
我已经尝试了附加部分。并且还创建一个新文件并将旧文件的内容移动到新文件中,但要删除的行除外。但这太潮了。我需要一些简单而优雅的东西。 :)
我正在尝试找到一种方法,用新行替换文件中包含字符串的行。
如果文件中不存在该字符串,则将其附加到文件中。
谁能给个示例代码?
编辑:如果我需要替换的行在文件末尾,是否还有?
【问题讨论】:
虽然我认识到这不是最聪明的做法,但以下代码逐行读取 demo.txt 并搜索单词 cactus 来替换它oranges,同时将输出写入名为 result.txt 的辅助文件。
别担心,我为你保存了一些工作。阅读评论:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string search_string = "cactus";
string replace_string = "oranges";
string inbuf;
fstream input_file("demo.txt", ios::in);
ofstream output_file("result.txt");
while (!input_file.eof())
{
getline(input_file, inbuf);
int spot = inbuf.find(search_string);
if(spot >= 0)
{
string tmpstring = inbuf.substr(0,spot);
tmpstring += replace_string;
tmpstring += inbuf.substr(spot+search_string.length(), inbuf.length());
inbuf = tmpstring;
}
output_file << inbuf << endl;
}
//TODO: delete demo.txt and rename result.txt to demo.txt
// to achieve the REPLACE effect.
}
【讨论】:
input_file 之后 getline 的状态。 Why is iostream::eof() inside a loop condition (i.e. while (!stream.eof())) considered wrong?
要替换文件中的最后两行:
fopen()打开文件fgetpos()获取当前文件位置
fseek() 定位到的位置
fprintf() 将新行添加到文件中,然后使用fclose() 将其关闭。【讨论】:
如前所述,KarlPhilip 的代码是一个很好的起点(谢谢),但根据 ">= 0" 不能正常工作。与“0”相比,它以 MS CString 类型和 C# 等而闻名,但它似乎不适用于 STL。例如。在 VS 2010(仅发行版)中,C++ 代码在此比较中表现错误而不会引发错误!
这是对 C++ 标准的更正:如果某些向导有改进,请发布。我认为,这是一个非常有用且重要的问题/sn-p。
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string search_string = "cactus";
string replace_string = "oranges";
string inbuf;
// Todo: Check, if input_file exists before.
// Todo: Try/catch or check, if you have write access to the output file.
fstream input_file("demo.txt", ios::in);
ofstream output_file("result.txt");
while (!input_file.eof())
{
getline(input_file, inbuf);
size_t foundpos = inbuf.find(search_string);
if(foundpos != std::string::npos)
{
string tmpstring = inbuf.substr(0,spot);
tmpstring += replace_string;
tmpstring += inbuf.substr(spot+search_string.length(), inbuf.length());
inbuf = tmpstring;
}
output_file << inbuf << endl;
}
//TODO: delete demo.txt and rename result.txt to demo.txt
// to achieve the REPLACE effect.
}
我在另一个 SO 问题的“if”中使用了以下替换部分 (上面的代码只替换了第一次出现):
if(foundpos != std::string::npos)
replaceAll(inbuf, search_string, replace_string);
//http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string
//
void replaceAll(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos)
{
size_t end_pos = start_pos + from.length();
str.replace(start_pos, end_pos, to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
【讨论】:
如果您想更改(或删除)文件中间一行的长度,您将不得不重新编写所有后续行。
没有办法简单地将字符删除或插入到现有文件中。
【讨论】: