如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值

How do I change the value of a string with the output of a file between the start of <x> and the end of <y>

本文关键字:gt lt 文件 字符串 输出 开头 结尾 之间      更新时间:2023-10-16

我正在尝试将字符串的值设置为OUTPUT,为文件中的某些文本。基本上在文件中,我想设置OUTPUT的值,其中包含两个字符串之间的所有文本TRACE,0000,Time,0

例如:

random text in the file

random text in the file

random text in the file

TRACE,0000

stuff

goes

in

here

Time,0

random text in the file

random text in the file

random text in the file

OUTPUT的值是:

TRACE,0000

stuff

goes

in

here

Time,0

我只是不知道如何处理这个问题。如果有人能帮助我,将不胜感激。

干杯。

以下示例代码可能会对您有所帮助。如代码所示,检查过程将在while-loop中消失。我希望它能帮助你。

#include<iostream>
#include<fstream>
#include<string>
int main()
{
// other things to do
std::fstream readFile;
readFile.open("file");
if (!readFile.is_open()){
std::cout<<"Files couldn't be open!";
return 1;
}
std::string line = "";
bool start_read = false;
while (getline(readFile, line)) {
if(line == "TRACE,0000")
start_read = true;
else if (line == "Time,0"){
std::cout<<line<<std::endl;
break;
}
if(start_read)
std::cout<<line<<std::endl;
}
// remaining things to do
return 0;
}