#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  std::ifstream fp;
  fp.open(/*filename.c_str()*/"aa.txt", std::ios::in | std::ios::binary);

  .seekg(0,FILE_END);

  streampos filesize = fp.tellg();
  char* str = new char[10];

if (fp.seekg(10))
{
  streampos size = fp.tellg();
  cout << size<<endl;
}

if (fp.seekg((DWORD)filesize +10))
{
  streampos size = fp.tellg(); //能够读取出来数据
  cout << size<<endl;    
  if (fp.getline(str, 10))
  {
    cout << "getline" << endl; //失败,没有进入,不能读取
  }
  if (fp.fail())
  {
    cout << "fp.fail()" << endl; //能够进入
  }
  if (fp.bad())
  {
    cout << "bad" << endl; //不进入
  }
}

delete[] str; return 0; }

结果:

 

 

结论:

在c++ 流文件中,seekg可以越界,需要通过fail()来判断。

不过在越界后,不能成功读取到数据,所以在读取数据前需要判断。 

posted on 2019-10-14 18:16  轻于飞  阅读(243)  评论(0)    收藏  举报