Simple Error problem..?

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
It's a warning, not an error. It thinks line 4 should be:
 
std::streamoff length = binaryio.tellg() / sizeof(std::streamoff);


Neither the compiler (or me) have any idea what you're doing. But if you want to check the file size, call stat() instead.
Yeah that did not work. The file I am dealing with is a Binary I/O file. What is the stat() method you are talking about?
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.

Regards
What is the stat() method you are talking about?

http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html
stat() accesses the file's metadata rather than the file itself, so it's more efficient as well as being easier to use.

Casting the result from binaryio.tellg() to int should fix it.
Don't do that, use the correct type. size_t might suffice, so could use that.
@kbw

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.
Topic archived. No new replies allowed.