View Single Post
Join Date: Nov 2008
Posts: 977
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: searching for keywords in multiple text files

 
0
  #9
Jan 10th, 2009
Originally Posted by skatamatic View Post
  1. ifstream inFile;
  2. vector<string> data;
  3. inFile.open("File.txt");
  4. while (!inFile.eof())
  5. {
  6. string sString = inFile.getline();
  7. data.push_back(sString);
  8. }
Don't bother with eof(), and don't put declarations in loops.
Simplified:
  1. ifstream file("name");
  2. vector<string> lines;
  3. string str;
  4.  
  5. if(file.is_open())
  6. {
  7. while(getline(file, str))
  8. {
  9. lines.push_back(str);
  10. }
  11.  
  12. file.close();
  13. }
  14. else
  15. {
  16. failed. set error events, logs, etc.
  17. }
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote