tellg and seekg c 2b 2b

Solutions on MaxInterview for tellg and seekg c 2b 2b by the best coders in the world

showing results for - "tellg and seekg c 2b 2b"
Sufyan
17 Jan 2016
1// read a file into memory
2#include <iostream>     // std::cout
3#include <fstream>      // std::ifstream
4
5int main () {
6  std::ifstream is ("test.txt", std::ifstream::binary);
7  if (is) {
8    // get length of file:
9    is.seekg (0, is.end);
10    int length = is.tellg();
11    is.seekg (0, is.beg);
12
13    // allocate memory:
14    char * buffer = new char [length];
15
16    // read data as a block:
17    is.read (buffer,length);
18
19    is.close();
20
21    // print content:
22    std::cout.write (buffer,length);
23
24    delete[] buffer;
25  }
26
27  return 0;
28}