write a program to find size of a file without traversing it character by character?
will anybody tell me the logic hw to approach it
is there any trick involved
write a program to find size of a file without traversing it character by character?
will anybody tell me the logic hw to approach it
is there any trick involved
Use fseek and ftell functions from <stdio.h>.
Every file ends with an EOF character.Find out what the functions which ArkM has stated does and use the above information and you are done.
> write a program to find size of a file without traversing it character by character?
Even using fseek()/ftell() might be an approximate answer.
http://c-faq.com/stdio/textvsbinary.html
are you using *nix?
#include <stdio.h>
#include <sys/stat.h>
int getFileSize(char *filename, long *filesize)
{
struct stat filestats;
if (stat(filename, &filestats) < 0) {
{
perror(filename);
return 0;
}
else
*filesize = filestats.st_size;
printf(" The size of %s is %ld bytes (%3.1f KB)\n", filename, *filesize, (*filesize / 1024.0) );
return 1;
}
.
Those C functions may not work with huge files -- files that are larger than 2 gig. In MS-Windows win32 api functions would be needed to get the file size of huge files. I don't know about *nix or MAC
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.