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

Re: reading a text file and searching for a sentence

 
0
  #11
Dec 25th, 2007
Originally Posted by Ancient Dragon View Post
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)
thanks dragon but i tried comparing the strings with a strcmp() function and it won't work

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
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: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading a text file and searching for a sentence

 
0
  #12
Dec 25th, 2007
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.
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
  #13
Dec 26th, 2007
hi dragon, sorry for the late reply

here's my code

  1. char str[5000];
  2. system("mycommand>output.exe");
  3. char error[]="the error line to be checked";
  4. fstream file_op("output.txt",ios::in);
  5.  
  6. while( file_op.getline(str,5000) )
  7.  
  8. {
  9. cout<<str<<endl;
  10. if(strcmp(error,str) == 0)
  11. {
  12. getchar();
  13. }
  14. {


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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: az1983us is an unknown quantity at this point 
Solved Threads: 1
az1983us az1983us is offline Offline
Newbie Poster

Re: reading a text file and searching for a sentence

 
0
  #14
Dec 30th, 2007
tanks a lot
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
  #15
Dec 31st, 2007
Originally Posted by az1983us View Post
tanks a lot
for what ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
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: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading a text file and searching for a sentence

 
0
  #16
Dec 31st, 2007
Originally Posted by hashinclude View Post
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!
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.
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
  #17
Jan 1st, 2008
Originally Posted by Ancient Dragon View Post
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.
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 :/
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
  #18
Jan 1st, 2008
Originally Posted by hashinclude View Post
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 :/
Then that obviously wasn't the problem. What I'd do now is display every character entered in hex just before comparing and, as AD suggests, see if there are differences you can't see.

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
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
  #19
Jan 1st, 2008
ok here is something i have tried :

  1. char str[500];
  2. char error[]="Windows IP Configuration"; //error string to find
  3. system("ipconfig>output.txt");
  4. fstream file("output.txt",ios::in);
  5.  
  6. int i=0,l=1,counter=0;
  7.  
  8. while( file.getline(str,500) )
  9. {
  10. cout<<"\nLine #"<<l<<":\n\n";
  11.  
  12. while(str[i]==error[i])
  13. {
  14. cout<<"[ "<<str[i]<<" ]"<<" = "<<"[ "<<error[i]<<" ]\n";
  15. i++;
  16. counter++;
  17. }
  18.  
  19. if(strcmp(error,str)==0 || counter==24)
  20. {
  21. cout<<"\nerror found!\n";cin.ignore();
  22. }
  23.  
  24. i=0;l++;counter=0;
  25. }
  26. file.close();
  27. return 0;

and this is the output:


  1. Line #1:
  2.  
  3.  
  4. Line #2:
  5.  
  6. [ W ] = [ W ]
  7. [ i ] = [ i ]
  8. [ n ] = [ n ]
  9. [ d ] = [ d ]
  10. [ o ] = [ o ]
  11. [ w ] = [ w ]
  12. [ s ] = [ s ]
  13. [ ] = [ ]
  14. [ I ] = [ I ]
  15. [ P ] = [ P ]
  16. [ ] = [ ]
  17. [ C ] = [ C ]
  18. [ o ] = [ o ]
  19. [ n ] = [ n ]
  20. [ f ] = [ f ]
  21. [ i ] = [ i ]
  22. [ g ] = [ g ]
  23. [ u ] = [ u ]
  24. [ r ] = [ r ]
  25. [ a ] = [ a ]
  26. [ t ] = [ t ]
  27. [ i ] = [ i ]
  28. [ o ] = [ o ]
  29. [ n ] = [ n ]
  30.  
  31. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: reading a text file and searching for a sentence

 
0
  #20
Jan 1st, 2008
Maybe you need to change:-

cout<<"\nerror found!\n";cin.ignore();

to

cout<<"\nMatch!\n";cin.ignore();

Or alternatively change:-

if(strcmp(error,str)==0

to

if(strcmp(error,str)==1


The choice is yours.
Last edited by iamthwee; Jan 1st, 2008 at 3:39 pm.
*Voted best profile in the world*
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