943,607 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8017
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 16th, 2007
0

reading a text file and searching for a sentence

Expand Post »
Hello,

i need help on how to search for a sentence inside a txt file after opening it and then applying a condition if that sentence was found.

im a beginner so i don't know how.

basically i have written a program that uses system("command>file.txt") to start a command line process and write the output to a file , and what i want is to search for a certain line in the output (an error message for example) and force the program to act upon the condition of it being found.

c++ Syntax (Toggle Plain Text)
  1. string sentencetolookup="error connecting to server";
  2. system("command>output.txt");

i want to know how to look for "sentencetolookup" inside the "output.txt" file and if found force the program to stop or return an error message.

any help is appreciated
thanks
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
Dec 16th, 2007
0

Re: reading a text file and searching for a sentence

you need an ifstream object to open the text file, and a string to receive the strings. Both of these are standard c++ classes in <fstream> and <string> header files. There are hundreds, if not thousands, of examples you can follow to write your program so I am not going to repeat them here. Basically you need to (1) read a line using getline function then (2) compare it to the string you are looing for using the == comparison operator.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 16th, 2007
0

Re: reading a text file and searching for a sentence

you need an ifstream object to open the text file, and a string to receive the strings. Both of these are standard c++ classes in <fstream> and <string> header files. There are hundreds, if not thousands, of examples you can follow to write your program so I am not going to repeat them here. Basically you need to (1) read a line using getline function then (2) compare it to the string you are looing for using the == comparison operator.
thanks, i did view alot of pages talking about ifstream, the best thing i could come up with is read a whole text file , i couldn't find anything on how to read specific lines or read all lines one by one and compare each to a string, all examples showed the use of an array as a buffer to read a txt file instead of a string when i compared an array to a string nothing happened

the whole reason im resorting to txt files is because i couldn't figure out how to obtain any returned results (error or successful completion) by other programs , i am using an exe that renames a computer (netdom) and i have a c++ program that would call that exe using the system() function but i want my program to terminate or loop back to the beginning if the exe file returned an error (like computer account already exists or anything of that sort)
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
Dec 16th, 2007
0

Re: reading a text file and searching for a sentence

You didn't look hard enough. Didn't you run across the getline() method?
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

i did look hard enough to find and use the getline() method , and i was able to read the whole txt file, but i didn't know where to go or what to do after that, how to lookup a sentence by searching each line in the txt file and then if it is found, tell my program do this and that
Last edited by hashinclude; Dec 17th, 2007 at 1:28 am.
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

Maybe this will help. Note the title...
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

in that case can anyone point me to where there is a very detailed tutorial on how to use ifstream ?
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

in that case can anyone point me to where there is a very detailed tutorial on how to use ifstream ?
I guess I was too subtle. Post your code as the link describes. Very often if we see the code we can figure out what to tell you. So far we're only guessing.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

c++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3.  
  4. char str[5000];
  5. char errorline[]="grab this text";
  6. system("command>output.txt");
  7. fstream file_op("output.txt",ios::in);
  8. while(!file_op.eof())
  9. {
  10. file_op.getline(str,5000);
  11. cout <<str;
  12. }
  13. if(str==errorline)
  14. {
  15. cout<<"\nerror found\n";
  16. return 0;
  17. }
  18.  
  19. file_op.close();
  20. system("pause");
  21. return 0;
  22.  
  23.  
  24. }

you can see that i can only know how to open the file and read it into str array but i don't know how to search the whole file or a certain line or all lines for a string of text that matches errorline
Reputation Points: 11
Solved Threads: 8
Junior Poster
hashinclude is offline Offline
111 posts
since May 2007
Dec 17th, 2007
0

Re: reading a text file and searching for a sentence

line 6: do you have a program on your computer called command.exe ? If not then that line of code will do nothing other than get an error message from the operating system that "command not found".

lines 8 and 10: that loop doesn't work right. You need to combine them like this:
C++ Syntax (Toggle Plain Text)
  1. while( file_op.getline(str,5000) )
  2. {
  3. // compare the strings here to see if this line is the one you are looking for
  4. }

line 13: you can not compare two c-style character arrays like that. Use strcpy() to do that
if( strcpy(errorline, str) == 0)
Last edited by Ancient Dragon; Dec 17th, 2007 at 9:03 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please advise
Next Thread in C++ Forum Timeline: instream and outstream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC