Mega Code Archive

 
Categories / C++ Tutorial / File Stream
 

Read and display a text file line by line

#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) {   ifstream in("test.txt");   if(!in) {     cout << "Cannot open input file.\n";     return 1;   }   char str[255];   while(in) {     in.getline(str, 255);  // delim defaults to '\n'     if(in) cout << str << endl;   }   in.close();   return 0; } R 9.9 T 9.9 M 4.8