How do I print out the contents of a file? C++ File Stream
#include <iostream>
#include <fstream>
int main()
{
string name ;
std::ifstream dataFile("file.txt");
while (!dataFile.fail() && !dataFile.eof() )
{
dataFile >> name ;
cout << name << endl;
}
There's no reason to reinvent the wheel here, when this functionality is already implemented in the standard C++ library.
#include <iostream>
#include <fstream>
int main()
{
std::ifstream f("file.txt");
if (f.is_open())
std::cout << f.rdbuf();
}