| | |
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:
Solved Threads: 0
•
•
•
•
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)
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)
this is what happens, if the txt file was created by the running program this way
system("somecommand>output.txt") then comparing the strings with a strcmp() function doesn't work [meaning that when the two compared strings do actually match, the program always decides that they don't match]but if i created the file myself and typed in it whatever i want to be compared, then strcmp() function would work.
Last edited by hashinclude; Dec 25th, 2007 at 10:56 am.
The code you posted a week ago is obviously not the same code you are using today. So post code (I can't see your monitor very well) and zip up the data file and post it too. stramp is case sensitive so maybe the string that's in the data file is not the same case as the string you want to compare. To do a case-insensive compare you can convert both strings to either upper or lower case before calling strcmp(). Some compilers have a case-insensitive compare function, such as stricmp() in Microsoft compilers and compareNoCase() (or something like that) in some other compilers.
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
hi dragon, sorry for the late reply
here's my code
the code compiles with no errors and the program runs with no errors , but the issue here is that
whats confusing is that when i create the output.txt file manually and not by having it created using the
here's my code
c++ Syntax (Toggle Plain Text)
char str[5000]; system("mycommand>output.exe"); char error[]="the error line to be checked"; fstream file_op("output.txt",ios::in); while( file_op.getline(str,5000) ) { cout<<str<<endl; if(strcmp(error,str) == 0) { getchar(); } {
the code compiles with no errors and the program runs with no errors , but the issue here is that
strcmp(error,str) always returns a value other than zero even if the lines compared match up !whats confusing is that when i create the output.txt file manually and not by having it created using the
system("mycommand>output.exe") function and then type in the the same output i would get from mycommand.exe , the strcmp(error,str) would then detect the matching lines properly! •
•
•
•
whats confusing is that when i create the output.txt file manually and not by having it created using thesystem("mycommand>output.exe")function and then type in the the same output i would get from mycommand.exe , thestrcmp(error,str)would then detect the matching lines properly!
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
•
•
•
•
That indicates the text generated by the system() function is not the same as what you type manually, could be as simple as a space, tab character, or capitalization. Load both files up into Notepad and compare them side-by-side to see what are the differences.
i tried removing spaces from the line created by the system() function and then using strcmp() but it still didn't work :/
•
•
•
•
although they look identical in notepad , i believe like you said, a space or tab character is added somewhere on each line by the system() function.
i tried removing spaces from the line created by the system() function and then using strcmp() but it still didn't work :/
You also might want to post part of the file.
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
ok here is something i have tried :
and this is the output:
you notice the first line in the file is an empty space
now the reason my error was found in the second line is because of the counter inside the
cpp Syntax (Toggle Plain Text)
char str[500]; char error[]="Windows IP Configuration"; //error string to find system("ipconfig>output.txt"); fstream file("output.txt",ios::in); int i=0,l=1,counter=0; while( file.getline(str,500) ) { cout<<"\nLine #"<<l<<":\n\n"; while(str[i]==error[i]) { cout<<"[ "<<str[i]<<" ]"<<" = "<<"[ "<<error[i]<<" ]\n"; i++; counter++; } if(strcmp(error,str)==0 || counter==24) { cout<<"\nerror found!\n";cin.ignore(); } i=0;l++;counter=0; } file.close(); return 0;
and this is the output:
C++ Syntax (Toggle Plain Text)
Line #1: Line #2: [ W ] = [ W ] [ i ] = [ i ] [ n ] = [ n ] [ d ] = [ d ] [ o ] = [ o ] [ w ] = [ w ] [ s ] = [ s ] [ ] = [ ] [ I ] = [ I ] [ P ] = [ P ] [ ] = [ ] [ C ] = [ C ] [ o ] = [ o ] [ n ] = [ n ] [ f ] = [ f ] [ i ] = [ i ] [ g ] = [ g ] [ u ] = [ u ] [ r ] = [ r ] [ a ] = [ a ] [ t ] = [ t ] [ i ] = [ i ] [ o ] = [ o ] [ n ] = [ n ] error found!
you notice the first line in the file is an empty space
now the reason my error was found in the second line is because of the counter inside the
if(!strcmp(error,str)||counter==24) statement even though you can see how the line and the error string match perfectly, !strcmp(error,str) still didn't see it O_O ![]() |
Other Threads in the C++ Forum
- Previous Thread: Please advise
- Next Thread: instream and outstream
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game generator getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






