When I run my program and I choose to see the product list, it doesn't print anything. After some time, I find out that the value of fl_size
is always 0. Why is this?
void view_prdct_code_list() {
FILE *stock = fopen("stock.dat","r+");
assert(stock);
int fl_size=ftell(stock);
int prd_size= sizeof(product);
int quantity= fl_size/prd_size;
printf("fl_size=%d",fl_size);
fseek(stock,0,SEEK_SET);
prdct cprd= (product *)malloc (sizeof(product)*quantity);
assert(cprd);
int i;
fread(cprd,prd_size,quantity,stock);
for (i=0;i<quantity;i++){
printf("PRODUCT CODE: %d\n",cprd->code);
}
free(cprd);
fclose(stock);
}
ftell
does not return the total size of the file; it returns the current read or write position within the file. You call ftell
immediately after opening the file, so that position is the very beginning of the file. You can either use fseek(stock, 0, SEEK_END)
to seek to the end before calling ftell
, or you can drop down a layer and use fstat(fileno(stock))
to retrieve the file size directly from the OS.
Further notes:
fread
is not guaranteed to read the entire file in one gulp even if you ask it to.ftell
returns a long
, not an int
.long
is only 32 bits, it is possible for a file to be so large that its size does not fit in a long
. If your program needs to handle files that big, you need to #define _FILE_OFFSET_BITS 64
at the top of every .c
file (before all the includes) and use fseeko
and ftello
. (Or whatever the Windows equivalent is.)"r+b"
.malloc
. (It is necessary to do this in C++, but in C it is not only unnecessary, it can hide bugs.)