如何解析一个 txt 文件,删除除我需要的所有行?
我想解析一个文本文件以获得我想要的内容,并用 C++ 创建另一个 txt 文件。 我有一个看起来像这样的文本文件。
User :
Group :
Comment1 :
Comment2 :
*** Label ***
ID : Nick
PASS : sky123
Number ID : 9402
*** End of Label ***
######################################
并继续。 我基本上想创建一个新的txt文件,其中保留包含冒号(:)的所有行并删除其余部分,例如“* Label *”,并将结果保存在新的txt文件中。 该 txt 文件的答案是
User :
Group :
Comment1 :
Comment2 :
ID : Nick
PASS : sky123
Number ID : 9402
如何以简单的方式执行此操作? 非常感谢。
I want to parse a text file to get what I want and create another txt file in c++.
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.
The answer to that txt file would be
User :
Group :
Comment1 :
Comment2 :
ID : Nick
PASS : sky123
Number ID : 9402
How do I do this in a simple way?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在带有
fstreams
的 C++ 中:In C++ with
fstreams
:我还没有测试过这个,但你明白了。
I have not tested this, but you get the idea.
我能想到的最简单的方法是逐行读取文本文件,仅存储您想要的行。读取完成后,打开一个单独的文件进行写入并写入存储的行。它不是 C++,但我写了一些伪代码来说明。
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.
从大的角度来说,您所要做的就是在一个循环中,检查每一行是否有 char (:),如果确实有,则将其添加到字符串中。最后只需将该字符串保存到新的文本文件中即可。
这是关于如何管理文件的教程
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. In the end just save that string into a new text file.
Here's a tutorial of how to manage files
好吧,我不写 C++,我不确定您是否正在寻求该语言的帮助,但抽象中这是一个简单的方法:
将文本文件加载到字符串中,然后将字符串拆分为数组中的数组。换行符,这样每个数组值就有 1 行文本文件。然后,迭代数组的每个元素。使用此方法,您可以检查单个行的内容,并使用字符串比较、正则表达式匹配等来确定是否要保留/修改该行。完成后,您只需将新字符串保存到文本文件即可。
或者,您可以使用全局查找/替换以及字符串比较、正则表达式等来立即将更改应用到整个文档,但这需要更高级的知识和正则表达式的应用,并且考虑到文档的大小可能不切实际。
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:
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.