1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// read file data using associated buffer's members
#include <iostream> // std::cout
#include <fstream> // std::filebuf, std::ifstream
int main () {
std::ifstream ifs ("test.txt", std::ifstream::binary);
// get pointer to associated buffer object
std::filebuf* pbuf = ifs.rdbuf();
// get file size using buffer's members
std::size_t size = pbuf->pubseekoff (0,ifs.end,ifs.in);
pbuf->pubseekpos (0,ifs.in);
// allocate memory to contain file data
char* buffer=new char[size];
// get file data
pbuf->sgetn (buffer,size);
ifs.close();
// write content to stdout
std::cout.write (buffer,size);
delete[] buffer;
return 0;
}
|