Try this sample program and build up on it:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void)
{
ifstream infile;
infile.open("football.txt", ios::in);
if (!infile)
{
cout<<"File does not exist";
return EXIT_FAILURE;
}
std::string text_from file;
while (getline(infile, text_from_file))
{
cout << text_from_file << "\n";
}
cout << "Paused. Press enter to continue.\n"
cin.ignore(10000, '\n');
return EXIT_SUCCESS;
}
Notice that text is being read from the file using the getline
function.
3
solved C++ : Creating program that reads a text file