fstream读取文件所有数据(tellg()\seekg())

// read a file into memory
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () 
{
    
    
    std::ifstream is ("test.txt", std::ifstream::binary);
    if (is)
    {
    
    
        // get length of file:
        is.seekg (0, is.end);
        int length = is.tellg();
        is.seekg (0, is.beg);
    
        // allocate memory:
        char * buffer = new char [length];
    
        // read data as a block:
        is.read (buffer,length);
    
        is.close();
    
        // print content:
        std::cout.write (buffer,length);
    
        delete[] buffer;
  }

  return 0;
}

猜你喜欢

转载自blog.csdn.net/xp178171640/article/details/108864346