Calculating average of all bytes in a given file in C : <p>I wrote the program but couldn't find the problem. Its reading file well but gives result 0. Did I do anything wrong on passing the file pointer?</p>

<pre><code>#include &lt;stdio.h&gt;
unsigned char average(const char *filename){
    unsigned int BLOCK_SIZE=512;
    unsigned int nlen=0, nround=0;
    unsigned char avg = 0;
    FILE *fp;
    unsigned char tmp[512];

    if ( (fp = fopen(filename,"r")) == NULL){
            printf("\nThe file did not open\n.");
            return 500;
    }

    while(!feof(fp)){
        if(fread(tmp, 1, BLOCK_SIZE, fp)){
            nlen+=BLOCK_SIZE;
            nround++;
        }else{
            BLOCK_SIZE=BLOCK_SIZE/2;
        }
    }

    avg=(unsigned char)(nlen/nround);
    return avg;
}
int main(void){
    printf(" The average of all bytes in the file : %d \n" ,average("v") );
    return 0;
}
`
</code></pre>
