RosettaCodeData/Task/Read-entire-file/C++/read-entire-file.cpp

24 lines
561 B
C++
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
2015-11-18 06:14:39 +00:00
int main( )
{
if (std::ifstream infile("sample.txt"))
{
// construct string from iterator range
std::string fileData(std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>());
cout << "File has " << fileData.size() << "chars\n";
// don't need to manually close the ifstream; it will release the file when it goes out of scope
return 0;
2013-04-10 23:57:08 -07:00
}
2015-11-18 06:14:39 +00:00
else
{
std::cout << "file not found!\n";
return 1;
2013-04-10 23:57:08 -07:00
}
}