Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900