Format
#include <io.h> long _filelength(int handle);
Language Level: Extension
_filelength returns the length, in bytes, of the
file associated with handle. The length of the file
will be correct even if you have the handle opened and have
appended data to the file.
Return Value
A return value of -1L indicates an error,
and errno is set to one of the following values:
| Value | Meaning |
| EBADF | The file handle is incorrect or the mode specified does not match the mode you opened the file with. |
| EOS2ERR | The call to the operating system was not successful. |
Example
This example opens a file and tries to
determine the current length of the file using _filelength.
#include <io.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h>
int main(void)
{
long length;
int fh;
printf("\nCreating sample.dat.\n");
system("echo Sample Program > sample.dat");
if (-1 == (fh = open("sample.dat", O_RDWR|O_APPEND))) {
printf("Unable to open sample.dat.\n");
return EXIT_FAILURE;
}
if (-1 == (length = _filelength(fh))) {
printf("Unable to determine length of sample.dat.\n");
return EXIT_FAILURE;
}
printf("Current length of sample.dat is %d.\n", length);
close(fh);
return 0;
/********************************************************
The output should be:
Creating sample.dat.
Current length of sample.dat is 17.
********************************************************/
}
![]()
_chsize -- Alter Length of
File
__eof -- Determine
End of File
<io.h>