gpt4 book ai didi

c++ - 我想删除 txt 文件中以 %% ( C++) 开头的一些行

转载 作者:行者123 更新时间:2023-11-28 05:04:30 25 4
gpt4 key购买 nike

我有一个文本文件,其中包含很多行,如下所示:

%% reason of the selling ?
%% they piggy-back on cingular 's service .
zone[-3] %% also , their t-zones ."
customer service[-2] %% since i received the phone.
%% 24 hours ?"
screen[-2] %% i must have heard this about a dozen times over the span"

我想删除所有以 %% 开头的行,还有一些中间包含 %% 的行,所以也删除 %% 直到这些行的末尾。我想要这样的最终结果

zone[-3]
customer service[-2]
screen[-2]

最佳答案

逐行读入临时字符串。检查前两个字符是否不等于 "%%" 并将字符串插入另一个文件:

#include <iostream>
#include <fstream>
#include <string>
int main(){
std::ifstream ifs("input.txt");
std::ofstream ofs("output.txt");
std::string tempstr;
while (std::getline(ifs, tempstr)){
if (tempstr.substr(0, 2) != "%%"){
ofs << tempstr << std::endl;
}
}
}

如果你想跳过任何位置有 "%%" 的行,修改上面的 if 语句为:

if (tempstr.find("%%") == std::string::npos)

关于c++ - 我想删除 txt 文件中以 %% ( C++) 开头的一些行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45161211/

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