reading a text file and searching for a sentence

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

reading a text file and searching for a sentence

 
0
  #1
Dec 16th, 2007
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: reading a text file and searching for a sentence

 
0
  #2
Dec 16th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: reading a text file and searching for a sentence

 
0
  #3
Dec 16th, 2007
Originally Posted by Ancient Dragon View Post
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)
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading a text file and searching for a sentence

 
0
  #4
Dec 16th, 2007
You didn't look hard enough. Didn't you run across the getline() method?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: reading a text file and searching for a sentence

 
0
  #5
Dec 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading a text file and searching for a sentence

 
0
  #6
Dec 17th, 2007
Maybe this will help. Note the title...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: reading a text file and searching for a sentence

 
0
  #7
Dec 17th, 2007
in that case can anyone point me to where there is a very detailed tutorial on how to use ifstream ?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading a text file and searching for a sentence

 
0
  #8
Dec 17th, 2007
Originally Posted by hashinclude View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 52
Reputation: hashinclude is an unknown quantity at this point 
Solved Threads: 0
hashinclude hashinclude is offline Offline
Junior Poster in Training

Re: reading a text file and searching for a sentence

 
0
  #9
Dec 17th, 2007
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: reading a text file and searching for a sentence

 
0
  #10
Dec 17th, 2007
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:
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC