user2117427
user2117427

Reputation: 487

C++ resolving istream::tellg warning

Warning:

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

Caused by:

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

Possible solution? Is it okay to do this:

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

How about this?

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

Upvotes: 12

Views: 10459

Answers (1)

us2012
us2012

Reputation: 16263

streamoff is a signed integral type defined by your C++ standard library implementation, and large enough to cater for the largest possible file sizes. In my x86_64 stdlibc++ for example, it is a int64_t.

In order to avoid potential data loss, use a larger type or... simply let your variable be of type streamoff.

Upvotes: 16

Related Questions