在QT中搜索txt文件 [英] search from txt file in QT

查看:100
本文介绍了在QT中搜索txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个网站以及其他许多网站..但我还没有任何解决方案。我的程序需要在文本文件中搜索单词,如果找到该单词,则打印/显示整行。示例文本文件具有以下内容



int DCDC_SyncGap_MRPDO [] = {5,0,0,0,0};

int ACDC_SyncGap_MRPDO [ ] = {1,1,0,0,0};



如果我的keywordString = DCDC程序在找到它时搜索文本DCDC。它返回以下完整的行。

int DcDc_SyncGap_MRPDO [] = {5,0,0,0,0};



如果我的keywordString = ACDC程序在找到它时搜索文本ACDC。它应该返回以下完整的行。

int ACDC_SyncGap_RPDO [] = {1,1,0,0,0};





任何建议和帮助代码都会受到赞赏。

I searched this site, as well as many other..but I'm not having any solution yet. My program needs to search for a word in a text file, and if it finds that word, Prints out/displays the entire line. example text file has the following

int DCDC_SyncGap_MRPDO[]={5,0,0,0,0};
int ACDC_SyncGap_MRPDO[]={1,1,0,0,0};

if my keywordString = DCDC Program searches text DCDC when it find this. it returns the following complete line.
int DcDc_SyncGap_MRPDO[]={5,0,0,0,0};

And if my keywordString = ACDC Program searches text ACDC when it find this. it should returns the following complete line.
int ACDC_SyncGap_RPDO[]={1,1,0,0,0};


any suggestion and helping code would be appreciated.

推荐答案

这与Qt无关,但与普通的C / C ++无关。



只需逐行读取文本文件,在行中搜索字符串,然后在找到时返回。因此,您必须选择可以逐行读取文本文件的文件方法。



这种方法可能是标准的C文件函数(fopen, fgets [ ^ ],fclose )或 istream :: getline [ ^ ]。然后使用标准C strstr [ ^ ]查看该行是否包含关键字的函数。请注意, strstr 区分大小写。如果你需要不区分大小写的搜索,你必须实现它。



因为你想返回匹配行的内容,所以应该使用某种动态字符串 QString



总体而言,这是C / C ++学生的典型练习。所以请原谅我懒得在这里写一些示例代码。
This is not related to Qt but plain C/C++.

Just read the text file line by line, search the line for the string, and return that when found. So you have to select a file method that can read text files by line.

Such a method may be the standard C file functions (fopen, fgets[^], fclose) or istream::getline[^] with C++ input streams. Then use the standard C strstr[^] function to look if the line contains the keyword. Note that strstr is case sensitive. If you need a case insensitive search, you must implement it.

Because you want to return the content of a matching line, some kind of dynamic string should be used like QString.

Overall this is a typical exercise for C/C++ students. So please excuse that I'm too lazy to write some example code here.


这篇关于在QT中搜索txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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