-5

Instead of writing cout hundreds of times, I want to read from a text file?

I don't get how to simply read from a text file, which only has words in it?

Many thanks!

Chris Beldam
  • 27
  • 1
  • 6

1 Answers1

-1

You can use this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main () {
    string line;
    ifstream myfile ("example.txt"); //change this to your file's name
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }

    else 
        cout << "Unable to open file"; 
}
Anwarvic
  • 12,156
  • 4
  • 49
  • 69