Discussion:
Problem setting file pointer (large files)
(too old to reply)
Chris
2008-01-23 17:42:22 UTC
Permalink
Hi,

I use this function which I have from MSDN, to handle file pointers in large
file sizes.

__int64 FilePosition (HANDLE hf, __int64 distance, DWORD MoveMethod)
{
LARGE_INTEGER li;
li.QuadPart = distance;
li.LowPart = SetFilePointer (hf, li.LowPart, \
&li.HighPart, MoveMethod);
if (li.LowPart == INVALID_SET_FILE_POINTER && \
GetLastError() != NO_ERROR)
{
li.QuadPart = -1;
}
return li.QuadPart;
}

My function uses it to occasionally set the file pointer back.

__int64 pos;

if ((pos = FilePosition(bitfile, 0L , FILE_CURRENT)) == -1)
{
printf("Unable to get file position.");
exit(1);
}

ReadFile(bitfile, &tmp, sizeof(unsigned char)*4, &nbytes, 0 );

/* checking some things */

if ((FilePosition(bitfile, pos, FILE_BEGIN)) == -1);
{
printf("Unable to set file position.");
exit(1);
}

Getting the file position and reading goes fine, I checked this for the
adresses,
but setting the pointer back consequently fails and comes up with the
"unable to set file position" error.
My source is C++ and runs from a thread in a dll. All additional reading
and writing goes fine except this little function.

Have someone any idea?

Thanks,
Chris.
Remy Lebeau (TeamB)
2008-01-23 18:21:21 UTC
Permalink
Post by Chris
if ((FilePosition(bitfile, pos, FILE_BEGIN)) == -1);
{
printf("Unable to set file position.");
exit(1);
}
Look very carefully at that 'if' statement? Notice anything that shouldn't
be there? There is an extra semicolon.

Also, just an FYI, it is not very efficient to use FILE_BEGIN in this manner
to seek all the way back to the beginning of the file and then seek forward
to the previous offset. You know exactly how many bytes were read. You
should use FILE_CURRENT with a negative offset instead of using FILE_BEGIN
when resetting the position, ie:

DWORD nbytes;

if( !ReadFile(bitfile, &tmp, sizeof(unsigned char)*4, &nbytes, 0) )
{
printf("Unable to read from file.");
exit(1);
}

// ...

if( FilePosition(bitfile, -((__int64) nbytes), FILE_CURRENT) == -1 )
{
printf("Unable to reset file position.");
exit(1);
}


Gambit
Chris
2008-01-24 01:25:40 UTC
Permalink
Yeah, I've seen it. I was blocking the return value that way I guess.

The suggestion to use the "bytes read" value from ReadFile() to move
just a offset from the current position is much better.
Works fine now!

Thanks,
Chris.
Post by Remy Lebeau (TeamB)
Post by Chris
if ((FilePosition(bitfile, pos, FILE_BEGIN)) == -1);
{
printf("Unable to set file position.");
exit(1);
}
Look very carefully at that 'if' statement? Notice anything that shouldn't
be there? There is an extra semicolon.
Also, just an FYI, it is not very efficient to use FILE_BEGIN in this manner
to seek all the way back to the beginning of the file and then seek forward
to the previous offset. You know exactly how many bytes were read. You
should use FILE_CURRENT with a negative offset instead of using FILE_BEGIN
DWORD nbytes;
if( !ReadFile(bitfile, &tmp, sizeof(unsigned char)*4, &nbytes, 0) )
{
printf("Unable to read from file.");
exit(1);
}
// ...
if( FilePosition(bitfile, -((__int64) nbytes), FILE_CURRENT) == -1 )
{
printf("Unable to reset file position.");
exit(1);
}
Gambit
Loading...