How can I find out the size of a file? I opened with an application written in C.
I would like to know the size, because I want to put the content of the loaded file into a string, which I alloc using malloc()
. Just writing malloc(10000*sizeof(char));
is IMHO a bad idea.
views:
39109answers:
10You need to seek to the end of the file and then ask for the position:
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
You can then seek back to the beginning:
fseek(fp, 0L, SEEK_SET);
There are two basic methods:
fseek(f, 0, SEEK_END); // seek to end of file
size = ftell(f); // get current file pointer
fseek(f, 0, SEEK_SET); // seek back to beginning of file
// proceed with allocating memory and reading the file
Or, you can use stat, if you know the filename:
struct stat st;
stat(filename, &st);
size = st.st_size;
If you're on Linux, seriously consider just using the g_file_get_contents function from glib. It handles all the code for loading a file, allocating memory, and handling errors.
If you have the file descriptor fstat() returns a stat structure which contain the file size.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// fd = fileno(f); //if you have a stream (e.g. from fopen), not a file descriptor.
struct stat buf;
fstat(fd, &buf);
int size = buf.st_size;
I've always used this method:
FILE *fd = fopen(...);
long size = filelength(fileno(fd));
if you're using file descriptors or
int f = open(...);
long size = filelength(f);
For low-level IO.
It seems like the simplest way to get a file's size. There's no need to do a couple of seeks.
For what you're doing though, you might want to consider using mmap and just map the file into your memory space.
#include <stdio.h>
#define MAXNUMBER 1024
int main()
{
int i;
char a[MAXNUMBER];
FILE *fp = popen("du -b /bin/bash", "r");
while((a[i++] = getc(fp))!= 9)
;
a[i] ='\0';
printf(" a is %s\n", a);
pclose(fp);
return 0;
}
HTH
Have you considered not computing the file size and just growing the array if necessary? Here's an example (with error checking ommitted):
#define CHUNK 1024
/* Read the contents of a file into a buffer. Return the size of the file
* and set buf to point to a buffer allocated with malloc that contains
* the file contents.
*/
int read_file(FILE *fp, char **buf)
{
int n, np;
char *b, *b2;
n = CHUNK;
np = n;
b = malloc(sizeof(char)*n);
while ((r = fread(b, sizeof(char), CHUNK, fp)) > 0) {
n += r;
if (np - n < CHUNK) {
np *= 2; // buffer is too small, the next read could overflow!
b2 = malloc(np*sizeof(char));
memcpy(b2, b, n * sizeof(char));
free(b);
b = b2;
}
}
*buf = b;
return n;
}
This has the advantage of working even for streams in which it is impossible to get the file size (like stdin).
#include <stdio.h>
int main(void)
{
FILE *fp;
char filename[80];
long length;
printf("input file name:");
gets(filename);
fp=fopen(filename,"rb");
if(fp==NULL) {
printf("file not found!\n");
}
else {
fseek(fp,OL,SEEK_END);
length=ftell(fp);
printf("the file's length is %1dB\n",length);
fclose(fp);
}
return 0;
}
To the OP: don't forget that sizeof(char) always evaluates to 1. Always.
if i want how many characters like "ABC" are in the file and if I use the lseek(fp,oL,2) as many of you told
if the file has many lines the result will not be the number of the characters ,,,
for example:
"file.txt"
ABC (new line) HGF
the method with the lseek will give 8 as result but the characters are 6.