1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// read a file into buffer - sgetn() example
#include <iostream> // std::cout, std::streambuf, std::streamsize
#include <fstream> // std::ifstream
int main () {
char* contents;
std::ifstream istr ("test.txt");
if (istr) {
std::streambuf * pbuf = istr.rdbuf();
std::streamsize size = pbuf->pubseekoff(0,istr.end);
pbuf->pubseekoff(0,istr.beg); // rewind
contents = new char [size];
pbuf->sgetn (contents,size);
istr.close();
std::cout.write (contents,size);
}
return 0;
}
|