is there a function to get lenght of a file in C
i am in linux and filelength would not work
is there a function to get lenght of a file in C
i am in linux and filelength would not work
Just create a file pointer like normal. Than use fseek to go to the end of the file. Then use ftell to let you know where it is. This should be correct for the file size. Just make sure to seek back to the beginning of the file before reading!
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
exactly what mr wizard said:
// might not be 101% correct
long length;
FILE *bleh = fopen ("somefile", "rb");
fseek (bleh, 0, SEEK_END);
length = ftell (bleh);
rewind (bleh);
// now start reading
hello, internet!
>This should be correct for the file size.
Don't expect an exact byte count though, this technique is only effective on binary files and even then the count may be approximate due to different end of line representations. Most of the time you don't really need to know the size of a file, just read it and take the size as you go.
-Prelude
My best code is written with the delete key.
Or use the (non-portable) stat function:
Code:#include <stdio.h> #include <sys/stat.h> int main(int argc, char *argv[]) { struct stat buf; if(stat(argv[0], &buf) == -1) { perror(argv[0]); return -1; } else { printf("File size = %ld bytes\n", buf.st_size); return 0; } }