当文件超过2G, 直接用tellg进行获取文件长度时,这时可能返回的是负值.
char szFilePath[_MAX_PATH] = {0};
sprintf(szFilePath, "%s", "D:\\Project\\Source\\StreamDataServer\\bin\\vod\\big.mp4");
fstream oStreamFile;
oStreamFile.open(szFilePath, ios_base::in);
oStreamFile.seekg(0, ios_base::end);
uint64_t nFileLen = (uint64_t)oStreamFile.tellg();
oStreamFile.close();
以上代码返回的是18446744071920692267
,即16进制的负值.
原因是文件超过2G时, tellg存在bug.
正确做法:
char szFilePath[_MAX_PATH] = {0};
sprintf(szFilePath, "%s", "D:\\Project\\Source\\StreamDataServer\\bin\\vod\\big.mp4");
fstream oStreamFile;
oStreamFile.open(szFilePath, ios_base::in);
oStreamFile.seekg(0, ios_base::end);
// uint64_t nFileLen = (uint64_t)oStreamFile.tellg();
// 兼容文件超过2G时的处理
__int64 size = *(__int64*) ( ((char*)&(oStreamFile.tellg())) +8);
oStreamFile.close();
References:
http://stackoverflow.com/questions/8007048/tellg-returning-negative-answer
http://www.cplusplus.com/reference/istream/istream/tellg/?kw=tellg