Read A Text File

This will open a text file and read it line by line. As it is, it will just print each line to the screen, so very much like the unix ‘cat’ command, but could very well do anything on the line just read by replacing the cout statement.


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

void main() {
    string s;
    ifstream infile;

    infile.open("aaa.txt");

    while(infile >> s) {
        cout << s << endl;
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.