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!
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!
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";
}