Reading from Text Files - C++ File Stream

C++ examples for File Stream:Text File

Description

Reading from Text Files

Demo Code

#include <cstdlib> 
 #include <iostream> 
 #include <string> 
 #include <fstream> 

 using namespace std; 

 int main(int argc, char *argv []) 
 { /*  ww w.  ja  v a2 s  .c om*/
    ifstream inputFile; 

    cout << "Opening input file..."; 
    inputFile.open("Input .txt"); 

    if (inputFile.is_open()) 
    { 
        cout << "Done" << endl << endl; 

        char inputString[80]; 

        cout << "Reading input file..." << endl; 
        cout << endl; 
        int i=0; 
        while (!inputFile.eof()) 
        { 
           inputFile.getline(inputString,80,'\n'); 
           cout << inputString << endl; 
           i++; 
        } 
        cout << endl; 
     } 
     else 
     { 
        cout << "Could not open input file." << endl; 
     } 
   
     return 0; 
}

Result


Related Tutorials