How do you get the file size for an open file?
-
I have the following code:
FILE \*fp = stdin; if ((fp = fopen(argv\[optind\], "rb")) == NULL) { printf("Cannot open file %s\\n", argv\[optind\]); return 2; }
It executes correctly and the correct file is accessed. I added the following to get the file size:
SizeLow = GetFileSize(\_fileno(fp), & SizeHigh); if ((SizeLow == 0xFFFFFFFF) && ((Error = GetLastError()) != NO\_ERROR)) { printf("Cannot read file size for %s, error %d\\n", argv\[optind\], Error); help(); return 2; } if (binary && (SizeHigh > 0x70000000)) { printf("File too big for Binary Mode\\n"); help(); return 2; } \_\_asm { mov eax,SizeLow mov edx,SizeHigh cmp binary,TRUE jnz Bytes shld edx,eax,3 shl eax,3 Bytes: mov DWORD PTR totalc,eax mov DWORD PTR totalc+4,edx }
When I execute I get the following:
ent.exe f:\tst1.tst >ent1.txt
Cannot read file size for f:\tst1.tst, error 6What am I doing wrong with the the GetFileSize call? In case you are wondering, this is a modification to John Walker's ENT to speed up execution for huge files. Dave.
-
I have the following code:
FILE \*fp = stdin; if ((fp = fopen(argv\[optind\], "rb")) == NULL) { printf("Cannot open file %s\\n", argv\[optind\]); return 2; }
It executes correctly and the correct file is accessed. I added the following to get the file size:
SizeLow = GetFileSize(\_fileno(fp), & SizeHigh); if ((SizeLow == 0xFFFFFFFF) && ((Error = GetLastError()) != NO\_ERROR)) { printf("Cannot read file size for %s, error %d\\n", argv\[optind\], Error); help(); return 2; } if (binary && (SizeHigh > 0x70000000)) { printf("File too big for Binary Mode\\n"); help(); return 2; } \_\_asm { mov eax,SizeLow mov edx,SizeHigh cmp binary,TRUE jnz Bytes shld edx,eax,3 shl eax,3 Bytes: mov DWORD PTR totalc,eax mov DWORD PTR totalc+4,edx }
When I execute I get the following:
ent.exe f:\tst1.tst >ent1.txt
Cannot read file size for f:\tst1.tst, error 6What am I doing wrong with the the GetFileSize call? In case you are wondering, this is a modification to John Walker's ENT to speed up execution for huge files. Dave.
GetFileSize()
expects a fileHANDLE
but you are passing a file descriptor returned by_fileno()
. This is indicated by the error code 6ERROR_INVALID_HANDLE
. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types. -
GetFileSize()
expects a fileHANDLE
but you are passing a file descriptor returned by_fileno()
. This is indicated by the error code 6ERROR_INVALID_HANDLE
. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types.Jochen, Thank you for the excellent reference. Just what I needed. Dave.
-
GetFileSize()
expects a fileHANDLE
but you are passing a file descriptor returned by_fileno()
. This is indicated by the error code 6ERROR_INVALID_HANDLE
. See A Handy Guide To Handling Handles[^] for the differences and how to convert between the various file handle types.Jochen, With this line, everything works correctly:
SizeLow = GetFileSize(\_get\_osfhandle(\_fileno(fp)), & SizeHigh);
Thanks for the pointer. Dave.