gpt4 book ai didi

c++ - 将文本文件 : C++ , 中的特定单词替换为预定义值

转载 作者:行者123 更新时间:2023-11-28 06:36:26 31 4
gpt4 key购买 nike

所以我有一个程序可以模拟 在编译器中的工作方式。输入程序是一个文件:说input.txt(文本文件)。

假设它有这样的内容:

#include<iostream>
#define PI 3.14

我想做的是,找到这个后面出现的PI这个词,并在文件中替换为3.14。我使用 FilePointer>>CharacterBuffer 从文件中逐字读取。完成所有这些后如何在文件中替换它?
谢谢。

最佳答案

您可以尝试使用以下代码替换文件中的字符串:-

    string line;
size_t len = stringToReplace.length(); //replace is a string to be replaced.
while (getline(in, line))
{
while (true)
{
size_t pos = line.find(stringToReplace);
if (pos != string::npos)
line.replace(pos, len, stringToReplace);
else
break;
}

out << line << '\n';
}

关于c++ - 将文本文件 : C++ , 中的特定单词替换为预定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701443/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com