gpt4 book ai didi

c++ - 如何编写字母数字文件解析器?

转载 作者:行者123 更新时间:2023-11-27 23:18:04 24 4
gpt4 key购买 nike

<分区>

到目前为止我有以下代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
ofstream outfile;
ifstream infile;
string line;

infile.open ("input.DAT");
outfile.open ("output.txt");

while (infile.good()){
getline (infile, line);
outfile << line << endl;
}

outfile.close();
infile.close();

return 0;
}

这一切所做的就是获取它的 input.DAT 并将其输出到 output.txt。但是,输入文件不干净。它采用这种格式:

(ASCII GARBAGE) 1:66 OS WARSAW, POLAND (ASCII GARBAGE)

示例图片:


另一个例子:


所以我想做的是输出垃圾之间的东西,换行符分隔。但我不知道如何按字符迭代/输出以及指示什么是有效输出的好方法(我的意思是我可以检查字符是否在我想的特定范围内但我不知道这是怎么回事在 C++ 中完成)。

我认为可能有帮助的方法是先搜索 (Number)(Number)(Colon)(Number)(Space) 或 (Number)(Colon)(Number)(Space) 形式的内容,然后取一切直到不是字母/逗号/句点/等的东西,并添加换行符。这能做到吗?

我希望这是有道理的!如果我需要澄清更多,请告诉我。

编辑:第一次尝试

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

int main(){
ofstream outfile;
ifstream infile;
string line, res;

infile.open ("input.DAT");
outfile.open ("output.txt");

while (infile.good()){
std::getline(infile, line);

res = "";
for(std::string::size_type i = 0; i < line.length()-4; i++){
if (isdigit(line[i+1]) && line[i+2]==":" && isdigit(line[i+3])){
res+=line[i];
i++;
while (isalnum(line[i]) || line[i] == "/" || line[i] == "\\" || line[i] == "=" || line[i] == "#" || line[i] == ":" || line[i] == " " || line[i] == "." || line[i] == "," || line[i] == "-" || line[i] == "'" || line[i] == '"'){
res+=line[i];
i++;
}
outfile << res << endl;
res = "";
}
}


}

outfile.close();
infile.close();

return 0;
}

虽然它没有编译,因为“ISO C++ 禁止指针和整数之间的比较”

编辑:我自己修复了这个问题,将引号更改为单引号。我想我在这里找到了自己的问题。不过,它不会让我删除我的问题。

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