| | |
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
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.
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:-
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:-
Here is a crappy solution using substrings, it might not even be want you want but at least it finds the line:-
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string str; string err = "Windows IP Configuration"; //error string to find system ( "ipconfig>output.txt" ); ifstream file ( "output.txt" ); while ( getline ( file, str, '\n' ) ) { cout << str << endl; int pos = str.find ( err ); //cout << pos << endl; if ( pos > -1 ) { cout << "\nerror found!\n"; cin.ignore(); } } file.close(); system ( "Pause" ); return 0; }
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:-
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string line; string err ="Windows IP Configuration"; //error string to find system("ipconfig>output.txt"); ifstream read("output.txt"); while( getline(read,line,'\n') ) { cout <<line<<endl; //int pos = str.find(err); //cout<<pos<<endl; if(line==err) { cout<<"\nerror found!\n"; cin.ignore(); } } read.close(); system("Pause"); return 0; }
Last edited by iamthwee; Jan 4th, 2008 at 1:38 pm.
*Voted best profile in the world*
•
•
•
•
i have a question iamthewee,
why useif ( pos > -1 )and notif ( pos != 0 )?
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*
![]() |
Other Threads in the C++ Forum
- Previous Thread: Please advise
- Next Thread: instream and outstream
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







