Mega Code Archive

 
Categories / C++ Tutorial / File Stream
 

Read text file line by line

#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]){    ifstream in("text.txt");    if(!in){       cout << "Cannot open file.";       exit (1);    }    char str[255];    while(in){       in.getline(str, 255);      // Delimiter defaults to newline       cout << str << endl;    }    in.close(); }