当我尝试获取文件大小时,我具有以下块来测试seekg和tellg的行为:
int size = 0;
ifstream in(fileName.c_str(), ifstream::in | ifstream::binary);
if(in)
{
in.seekg(0,ifstream::end);
size = in.tellg();
cout << endl;
cout << endl;
cout << "********** size stream1*** =" << size << endl;
in.seekg(0,ios::end);
size = in.tellg();
cout << "********** size stream2*** =" << size << endl;
in.seekg(0,ios::end);
size = in.tellg();
cout << "********** size stream3*** =" << size << endl;
in.seekg(100,ios::end);
size = in.tellg();
cout << "********** size stream4*** =" << size << endl;
in.seekg(0,ios::beg);
size = in.tellg();
cout << "********** size stream5*** =" << size << endl;
in.seekg(100);
in.seekg(0, ios::end);
size = in.tellg();
cout << "********** size stream6*** =" << size << endl;
in.seekg(100);
in.seekg(0, ios::cur);
size = in.tellg();
cout << "********** size stream7*** =" << size << endl;
in.seekg(-100,ios::end);
size = in.tellg();
cout << "********** size stream8*** =" << size << endl;
in.seekg(ios::beg,ios::end);
size = in.tellg();
cout << "********** size stream9*** =" << size << endl;
in.seekg(ios::beg);
in.seekg(ios::end);
size = in.tellg();
cout << "********** size stream10*** =" << size << endl;
cout << endl;
cout << endl;
}
其结果如下:
********** size stream1*** =1846
********** size stream2*** =1846
********** size stream3*** =1846
********** size stream4*** =1946
********** size stream5*** =0
********** size stream6*** =1846
********** size stream7*** =100
********** size stream8*** =1746
********** size stream9*** =1846
********** size stream10*** =2
我的问题如下:
好,我们开始:
tellg()
将产生位置对象,可用于返回到您所在的位置。返回的类型std::streampos
是std::fpos<std::mbstate_t>
。但是,std::fpos<std::mbstate_t>
将a转换为std::streamoff
整数。本质上,astd::streampos
转换为从第一个位置到当前位置的字符数。0
相对于字符std::ios_base::beg
的位置将位置明确设置为文件的开头。您是要使用std::ios_base::cur
吗?seekg()
将是相对于开始,当前位置,或者根据端部何处参数之中std::ios_base::beg
,std::ios_base::cur
或std::ios_base::end
。std::ios_base::seekdir
类型是一个枚举,并std::ios_base::end
可能具有价值2
。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句