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
  #41
Jan 4th, 2008
ok check my post#36 , some character is added by system() to each line , the first line is supposed to be an empty line, but it contains that character, maybe if we strip that character out of each line before comparing the line that would fix the problem , i don't know how to do it though
Last edited by hashinclude; Jan 4th, 2008 at 2:53 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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

 
1
  #42
Jan 4th, 2008
No Ancient Dragon, I'm afraid you are still wrong. I don't mean to be rude, but you do not understand the nature of the problem. I admit I didn't at first either until I actually opened the file created by ipconfig for further inspection.

Here is a crappy solution using substrings, it might not even be want you want but at least it finds the line:-

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string str;
  9.  
  10. string err = "Windows IP Configuration"; //error string to find
  11. system ( "ipconfig>output.txt" );
  12. ifstream file ( "output.txt" );
  13.  
  14. while ( getline ( file, str, '\n' ) )
  15. {
  16. cout << str << endl;
  17.  
  18. int pos = str.find ( err );
  19. //cout << pos << endl;
  20. if ( pos > -1 )
  21. {
  22. cout << "\nerror found!\n";
  23. cin.ignore();
  24. }
  25. }
  26. file.close();
  27. system ( "Pause" );
  28. return 0;
  29. }



Notice the problem if you just try to compare the line (it doesn't match) even though when you open up the text file it looks like it should:-

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string line;
  9.  
  10. string err ="Windows IP Configuration"; //error string to find
  11. system("ipconfig>output.txt");
  12. ifstream read("output.txt");
  13.  
  14.  
  15. while( getline(read,line,'\n') )
  16. {
  17. cout <<line<<endl;
  18.  
  19. //int pos = str.find(err);
  20. //cout<<pos<<endl;
  21. if(line==err)
  22. {
  23. cout<<"\nerror found!\n";
  24. cin.ignore();
  25. }
  26.  
  27. }
  28. read.close();
  29. system("Pause");
  30. return 0;
  31. }
Last edited by iamthwee; Jan 4th, 2008 at 1:38 pm.
*Voted best profile in the world*
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
  #43
Jan 5th, 2008
thanks iamthwee , your solution is not crappy at all its good , it does find the error line and that is exactly what i want it to do

thank you so much
Last edited by hashinclude; Jan 5th, 2008 at 3:42 pm.
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
  #44
Jan 6th, 2008
i have a question iamthewee,

why use if ( pos > -1 ) and not if ( pos != 0 ) ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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
  #45
Jan 6th, 2008
Originally Posted by hashinclude View Post
i have a question iamthewee,

why use if ( pos > -1 ) and not if ( pos != 0 ) ?
You can try it yourself and see what happens. But the short answer is it won't work.

Basically:-
If there isn't a match pos will be -1.
If there is a match it will return the character offset in that string.
*Voted best profile in the world*
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
  #46
Jan 6th, 2008
i see, let me will check it out
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