Since nobody answered the thread on Binary File IO, I took a different approach.

Now I'm having another problem.

As I process a very large (>2GB) binary file, I store the byte position at certain points. Then, I reposition the file as needed.

I store the byte positions in a variable declared:

System.Int64 curr_pos;

However, the program fails when I position the file:

infile.Position = curr_pos;

I debug, and notice that this fails when the value of curr_pos gets over 2,147,483,647, which is the limit of a 32-bit integer. In fact, the value of curr_pos becomes negative, and that's the error I'm getting, that the value must be non-negative.

Why, since I explicitly declare the variable as 64-bit, does it fail when it reaches the 32-bit limit?

...and the answer to this is that the curr_pos was calculated, and one of the terms in the calculation was an Int32, casting the entire result as an Int32.

Each term has to be cast to Int64 to maintain the integral type of the result.

...and the answer to this is that the curr_pos was calculated, and one of the terms in the calculation was an Int32, casting the entire result as an Int32.

Each term has to be cast to Int64 to maintain the integral type of the result.

This is the opposite to almost all (typed and psuedo typed) languages, and probably not exactly what is occuring. The type of an equation usually becomes the type of the largest type used in the equation.

So ...

long a  = 30000L;
short b = a * 3;

Will not compile because (a * 3) is of type long.. and you cannot implicitly cast down (in most languages). In C/C++, writing code like this will compile... however these langauges presume that you know what you are doing.

So what happens is the equation is cast to the larger type, but then when it comes to assignment you will suffer data loss (the answer to the above equation is 24464).

Likewise a simple way of forcing cast of an equation to double is to multiply by 1.0.

i.e. 3*1.0/4 = 0.75, but 3/4 = 0.

This article has been dead for over six months. Start a new discussion instead.