如何将文件复制到另一个文件中,但是用用户输入的单词替换单词
How to copy a file into another file but replace a word with a user entered word?
我正在尝试将文件复制到另一个文件,但是用用户输入的内容更改单词。到目前为止,我已经想到了:
while (getline(openningTheFile, line, ' ')) //line is a string and openningTheFile is an ifstream
{
if (line == wordToBeDeleted)
{
line = wordToReplaceWith;
}
if (line == "n")
{
newFile << endl; //newFile is an ofstream
}
newFile << line << " ";
}
,但问题在于此代码在"n"
之后没有读取单词,因为定界符是空格。
有人可以将我指向正确的方向吗?
策略我建议:
- 使用
std::getline
读取线路。 - 寻找您想使用
std::string::find
在行中替换的字符串。 - 如果找到了,请用新字符串替换。
- 重复步骤2和3,直到找不到字符串为止。
- 输出更新的行。
这是为此的核心代码:
while (getline(openningTheFile, line))
{
std::string::size_type pos;
while ( (pos = line.find(wordToBeDeleted)) != std::string::npos )
{
line.replace(pos, wordToBeDeleted.length(), wordToReplaceWith);
}
newFile << line << 'n';
}
您正在使用std::getline
读取文本的 line 。
您需要在文本行中找到单词,替换单词,然后将文本行写入输出文件。
一种方法是使用std::stringstream
和操作员>>
从字符串中提取单词。
另一个是使用std::string::find
定位单词的位置。
您可以通过修改循环以使用 std::istringstream
读取线条读取单词来做到这一点:
while (getline(openningTheFile, line))
{
std::istringstream iss(line);
std::string word;
bool firstWord = true;
while(iss >> word)
{
if(word == wordToBeDeleted) {
newFile << wordToReplaceWith;
if(!firstWord) {
newFile << " ";
}
firstWord = false;
}
}
newFile << `n`;
}
这是一种解决方案,它使用boost :: iostreams的力量以高级但仍然非常灵活的方式解决任务。
对于OP的情况,这可能就像使用大锤锤破裂螺母一样,但是如果需要灵活性或必须处理更复杂的情况,则可能是正确的工具。
我正在使用与正则表达式结合的过滤流。这使我们可以在不创建任何中间字符串的情况下用替换字符串替换搜索模式。
#include <iostream>
#include <string>
#include <sstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/regex.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/regex.hpp>
namespace io = boost::iostreams;
using namespace std;
int main()
{
// Your input "file" - you may replace it by an std::ifstream object.
istringstream in( "why waste time learning learninglearning, when ignorance is instantaneous?" );
// Search pattern and substitution string.
string findRegEx( R"(blearningb)" );
string replaceStr( "sleeping" );
// Build a regular expression filter and attach it to the input stream.
io::filtering_istream inFiltered;
inFiltered.push( io::regex_filter( boost::regex( findRegEx ), replaceStr ) );
inFiltered.push( in );
// Copy the filtered input to the output, replacing the search word on-the-fly.
// Replace "cout" by your output file, e. g. an std::ofstream object.
io::copy( inFiltered, cout );
cout << endl;
}
live demo
输出:
why waste time sleeping learninglearning, when ignorance is instantaneous?
注意:
- 实际的正则表达式为
blearningb
- 我们不需要逃脱后斜线,因为我们使用了一个原始字符串字面。这样的东西非常整洁。
- 正则表达式搜索整个单词"学习"(
b
表示一个单词边界)。这就是为什么它仅取代"学习"而不是"学习学习"的第一次事件。
相关文章:
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 我们如何在文本文件中找到C++中的几个单词?
- 是否可以在文本文件中找到最长单词的长度,并在同一个文本文件中读取,只需 1 个 while 循环?
- 按单词搜索文件,然后在C++中打印特定行数
- 如何检查流中文件的下一个单词是否为 alpha?
- 从文件中查找单词并替换到其他文件中
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- 如何在 c++ 中从文本文件中获取特定单词?
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 从之前添加的批处理文件中删除单词
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- C++ 如何检查哪些单词不在 2 个类似文件中
- 显示文件中具有最小给定元音数的单词
- 简单的二分搜索对我不起作用。我正在尝试从文本文件中搜索单词
- 使用结构数组计算文本文件中单词的出现次数C++
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 将文本文件单词插入 AVL