Sunday, January 24, 2016

Reading in a Text File in C++

A simple program that I wrote before in C++ to read a text file. The code is very short and easy to understand.

If you have some questions please send me an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.


Program Listing


#include <iostream>
#include <fstream>

using namespace std;

main() {

     string line;
     ifstream a("test.txt");

     while(!a.eof()) {
         if (a.is_open() == false) {
             cout << "Unable to open the file";
             break;
         }
         getline(a,line);
         cout << "\n" << line;
     }


     a.close();
     cout << "\n\n";
     system("pause");
}


No comments:

Post a Comment