简体   繁体   English

如何解析一个txt文件,该文件会删除除我需要的所有行之外的所有行?

[英]How do I parse a txt file which erases all the lines except what I need?

I want to parse a text file to get what I want and create another txt file in c++. 我想解析一个文本文件以获取想要的内容,并在c ++中创建另一个txt文件。 I have a text file which looks something like this. 我有一个看起来像这样的文本文件。

User :

Group : 

Comment1 :

Comment2 :

*** Label ***

ID : Nick

PASS : sky123

Number ID : 9402

*** End of Label ***

######################################

And goes on. 并继续。 I basically want to create a new txt file which leaves all lines which contains colon(:) and erase the rest such as " * Label * ", and save the result in a new txt file. 我基本上想创建一个新的txt文件,该文件将保留包含冒号(:)的所有行,并删除其余的行,例如“ * Label * ”,然后将结果保存在新的txt文件中。 The answer to that txt file would be 该txt文件的答案是

User :

Group :

Comment1 :

Comment2 :

ID : Nick

PASS : sky123

Number ID : 9402

How do I do this in a simple way? 我如何以一种简单的方式做到这一点? Thank you very much. 非常感谢你。

In C++ with fstreams : 在带有fstreams C ++中:

ifstream input("input.txt");
ofstream output("output.txt");

string line;

while (getline(input, line)) {
    if (line.find(":") != string::npos) {
        output << line << "\n";
    }
}

I have not tested this, but you get the idea. 我没有测试过,但是您知道了。

void foo(ifstream& ifs, ofstream& ofs)
{
    while ( !ifs.eof() )
    {
        string line;
        getline(ifs, line);
        if (line.find(":") != string::npos)
            ofs << line;
    }
}

The simplist approach I can think of would be to read in the text file line by line storing only the lines you want. 我能想到的简单列表方法是逐行读取文本文件,仅存储所需的行。 Once the read is complete, open a separate file for writing and write the stored lines. 读取完成后,打开一个单独的文件进行写入并写入存储的行。 It's not C++ but I've written out some psuedo code to illustrate. 它不是C ++,但是我写了一些伪代码来说明。

while(line in source file)
{
    if(wantline)
       store the line
}
for(stored lines)
   write line to destination file

In a big way, all you have to do is in a loop, check for each line if it have the char (:), if it does have it, add it to a string. 在很大程度上,您要做的就是循环处理,检查每行是否有char(:),如果有,则将其添加到字符串中。 In the end just save that string into a new text file. 最后,只需将该字符串保存到新的文本文件中即可。

Here's a tutorial of how to manage files 这是有关如何管理文件的教程

Well I don't write C++ and I'm not sure if you're looking for help with the language, but here's a simple approach in the abstract: 好吧,我不写C ++,也不确定您是否正在寻找有关该语言的帮助,但是以下是抽象中的一种简单方法:

Load the text file into a string and then split the string into an array on the newline character so that you have 1 line of the text file per array value. 将文本文件加载到字符串中,然后将字符串拆分到换行符上的数组中,以便每个数组值具有一行文本文件。 Then, iterate over each element of the array. 然后,遍历数组的每个元素。 Using this method you can examine the contents of an individual line and use string comparison, regex matching, etc. to determine whether you want to keep/modify that line. 使用此方法,您可以检查单个行的内容,并使用字符串比较,正则表达式匹配等来确定是否要保留/修改该行。 Once you're done you can simply save the new string to a text file. 完成后,您只需将新字符串保存到文本文件即可。

Alternately, you can use global find/replace with string comparison, regex etc. to apply changes to the entire document at once, but that requires more advanced knowledge and application of regex, and may not be practical given the size of the document. 或者,您可以将全局查找/替换与字符串比较,正则表达式等一起使用,以一次将更改应用于整个文档,但这需要更高级的知识和正则表达式的应用,并且鉴于文档的大小,这可能不切实际。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM