C ++解析istream :: tellg警告

时间:2013-03-02 08:12:31

标签: c++

警告:

warning C4244: 'initializing' : conversion from 'std::streamoff' to 'unsigned int', possible loss of data

引起:

unsigned int FileSize = File.tellg( ); // WARNING
std::cout << "Size = " << FileSize << std::endl;

可能的解决方案? 可以这样做:

// No more warnings but, is it safe?
unsigned int FileSize = (unsigned int)File.tellg( ); // OK?
std::cout << "Size = " << FileSize << std::endl;

这个怎么样?

// No more warnings but, is it safe?
unsigned int FileSize = static_cast< unsigned int >( File.tellg( ) );

1 个答案:

答案 0 :(得分:13)

streamoff是由C ++标准库实现定义的有符号整数类型,大小足以满足最大可能的文件大小。例如,在我的x86_64 stdlibc ++中,它是int64_t

为了避免潜在的数据丢失,请使用更大的类型或...只需让您的变量属于streamoff类型。

相关问题