Mega Code Archive

 
Categories / C++ Tutorial / File Stream
 

Read text file token by token

#include <iostream> #include <fstream> #include <string> using namespace std; int main() {   ifstream inFile("test.cpp");   if (inFile.fail()) {     cerr << "Unable to open file for reading." << endl;     exit(1);   }   string nextToken;   while (inFile >> nextToken) {     cout << "Token: " << nextToken << endl;   }   inFile.close();   return 0; }