views:

72

answers:

3

I have really old 'c' code that uses read to read a binary file. Here is a sample:

uint MyReadFunc(int _FileHandle, char *DstBuf, uint BufLen)
{  
    return (read( _FileHandle, DstBuf, BufLen));
}

For 64bit OS - char * will be 64 bits but the BufLen is only 32 bits and the returned value are only 32 bits.

Its not an option to change this to .NET - I have .NET versions, but I need this old library converted also.

Can someone please tell me what I need to use to do File i/o on 64 bit OS (using 'C' code)

+3  A: 

Use size_t, not uint.

Ignacio Vazquez-Abrams
You mean `ssize_t`, since `read` can return `-1`. Assuming POSIX `read` of course.
Alok
Yes, the return type should be `ssize_t`. I had missed that; I meant that `BufLen` should be a `size_t`.
Ignacio Vazquez-Abrams
I am using Windows Vista, Visual Studio 2005 - the prototype for read is:int read(int fileHandle, void * buffer, unsigned int buffSize)Not size_t - I agree it "should" be size_t, but it isn't.
AnnD
A: 

It looks like you're conflating two things: size of a pointer and the extent of the memory it points to.

just somebody
A: 

I'm not sure about char* being 64-bits - the pointer itself will be 64-bit, yes, but the actual value is still a character array, unless I'm missing something? (I'm not a brilliant C programmer.)

The length argument to read() is size_t, not int, which on a 64-bit system should be 64-bit not 32. Also the return value is a ssize_t, not int, which will also be 64-bit so you should be covered if you just change your function definition to return ssize_t, and take size_t instead of the ints.

Andy Shellam
I am using Windows Vista, Visual Studio 2005 - the prototype for read is:int read(int fileHandle, void * buffer, unsigned int buffSize)Not size_t - I agree it "should" be size_t, but it isn't.
AnnD