我必须读取一个文本文件并查找特定的子字符串并在另一个文件中打印。 [英] I have to read a text file and look for a specific substring and print in another file.

查看:63
本文介绍了我必须读取一个文本文件并查找特定的子字符串并在另一个文件中打印。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是文本文件的摘录。

要搜索的子字符串是主题短

我需要在另一个文件中打印,主题名称这是在子字符串之后的引号内,也是它旁边的id。



< subject short =Savremeni oksidni i neoksidni materijali-Vname =ICM47-Vid =D44D06A2ED25C9C3partner_id =/> < br $> b $ b

< subject short =Biokeramicki materijali-Pname =ICM49-Pid =CD0687B09D5CE7C8partner_id =/>



< subject short =Biokeramicki materijali-Vname =ICM49-Vid =E9E661064763C3D9partner_id =/>



< subject short =Korozijaizaštitamaterijala-Vname =14IIM41-Vid =5AF2BF5C810D5D98partner_id =/>



< subject short =Uvod uhemijskoinženjerstvo-Vname =14HHI210-Vid =8CE923B31C35DB61partner_id =/>







我的尝试:



我试过这样做,但似乎没有任何工作。

解决方案

你必须解析文本。您可以使用原始 strtok [ ^ ]调用或正则表达式库。请注意,由于 C ++ 11 ,正则表达式是 STL 的一部分:<regex> - C ++参考 [ ^ ]。

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!


引用:

我试过做它似乎没什么用。

我只能假设你没有做正确的事情。

选择正确的函数或类来读取文件是第一件事。所以首先编写代码来做到这一点。从文件中读取每一行并将其显示在控制台或窗口上,具体取决于您编写的程序类型。完成后,添加代码以检查每行所需的关键字,并仅显示具有正确关键字的行。一旦你有了工作,然后添加代码来分割线条,只显示你想从每一行的内容。逐个构建您的应用程序,您会发现它更容易。


this is an excerpt from the text file.
the substring to searched for is 'subject short'
I need to print in another file, name of the subject which is within the quotes after the substring and also the id next to it.

<subject short="Savremeni oksidni i neoksidni materijali-V" name="ICM47-V" id="D44D06A2ED25C9C3" partner_id=""/>

<subject short="Biokeramicki materijali-P" name="ICM49-P" id="CD0687B09D5CE7C8" partner_id=""/>

<subject short="Biokeramicki materijali-V" name="ICM49-V" id="E9E661064763C3D9" partner_id=""/>

<subject short="Korozija i zaštita materijala-V" name="14IIM41-V" id="5AF2BF5C810D5D98" partner_id=""/>

<subject short="Uvod u hemijsko inženjerstvo-V" name="14HHI210-V" id="8CE923B31C35DB61" partner_id=""/>



What I have tried:

I have tried doing it, but nothing seems to work.

解决方案

You have to parse the text. You may either use raw strtok[^] calls or a regex library. Please note, since C++ 11, regular expressions are part of the STL: <regex> - C++ Reference[^].


We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


Quote:

I have tried doing it, but nothing seems to work.

I can only assume you did not do the right things.
Selecting the correct functions or classes to read the file is the first thing to do. So start by writing the code to do just that. Read each line from the file and display it on the console or window, depending on the type of program you are writing. Once you have that working then add the code to check each line for the keywords you want, and display just the lines with the correct keywords. Once you have that working, then add the code to split the lines and display just the content that you want from each line. Build your application piece by piece and you will find it much easier.


这篇关于我必须读取一个文本文件并查找特定的子字符串并在另一个文件中打印。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆