| | |
reading a text file and searching for a sentence
Thread Solved
![]() |
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
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.
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
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)
string sentencetolookup="error connecting to server"; 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
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.
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
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.
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)
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: May 2007
Posts: 52
Reputation:
Solved Threads: 0
c++ Syntax (Toggle Plain Text)
int main() { char str[5000]; char errorline[]="grab this text"; system("command>output.txt"); fstream file_op("output.txt",ios::in); while(!file_op.eof()) { file_op.getline(str,5000); cout <<str; } if(str==errorline) { cout<<"\nerror found\n"; return 0; } file_op.close(); system("pause"); return 0; }
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
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:
line 13: you can not compare two c-style character arrays like that. Use strcpy() to do that
lines 8 and 10: that loop doesn't work right. You need to combine them like this:
C++ Syntax (Toggle Plain Text)
while( file_op.getline(str,5000) ) { // compare the strings here to see if this line is the one you are looking for }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Please advise
- Next Thread: instream and outstream
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






