I keep on getting this error with this simple piece of code.
1 2 3 4 5
fstream binaryio; // Create stream object
binaryio.open("Exercise13_5.dat", ios::in | ios::binary);
binaryio.seekg (0, ios::end);
int length = binaryio.tellg() / sizeof(int);
binaryio.seekg(0); //to return the file pointer to the beginning
c:userskraigballadesktopcollegeisuspring 2011cs 182examplebinaryarrayio.cpp(36): warning C4244: 'initializing' : conversion from 'std::streamoff' to 'int', possible loss of data
Casting the result from binaryio.tellg() to int should fix it. It should not be a problem in most circumstances and particularly when you can anticipate that the size of the file will never be greater than what fits inside an integer.
Using size_t is probably better for this expression, I agree. But eventually, you index the array with int-s. So, either the issue has to be resolved on this line in order to make sure that the result fits in an int, and therefore can be indexed with int-s later, or you need to adjust both the indexes and the length variable to use some larger more expressive type (long long). I doubt that anyone uses size_t to index, despite that it may be the better way. It is too much signed/unsigned casting hassle.