This function returns the size of a file, measured in bytes.

Syntax

long fsize(
  char *filename
);

Parameters

filename
[in] Points to a string containing the name of the file whose size is to be determined.

Return value

Returns the number of bytes in the file size, or ERROR.
Value Description
File size The size of the file.
ERROR The file cannot be found.

Remarks

Even if you perform fflush during fopen, the file size by fsize does not change.
The size is fixed by performing fclose.

Requirements

Header file:
lib.h
Library file:
libSTARTUPOPH5000.a

Sample

#include <stdio.h>
#include "lib.h"

void main(void)
{
    long size;
    FILE *fp;
    static char data[20+1];
    unsigned int recsize;

    for (recsize = 0; recsize < 20; recsize++)  // prepare some data
    {
        data[recsize] = 'a' + recsize;  // quit when recsize == 20
    }

    data[recsize] = '\0';
    printf("\fPress a key");

    for(;;)
    {
        ResetKey();
        while (!kbhit())
        {
            Idle();
        }

        if (NULL == (fp = fopen("TEST.DAT","r+b")))
        {
            if (NULL == (fp = fopen("TEST.DAT","w+b")))
            {
                printf("\fError opening\nTEST.DAT");
                continue;
            }
        }
        if (fseek(fp, 0L, SEEK_END) != 0)
        {
            fclose(fp);
            printf("\fError fseek on\nTEST.DAT");
            continue;
        }
        if (fwrite(data, 1, recsize, fp) != recsize)
        {
            fclose(fp);
            printf("\fError writing\nTEST.DAT");
            continue;
        }
        fclose(fp);

        size = fsize("TEST.DAT");
        if (size > -1)
        {
            printf("\fSize = \n%ld bytes", size);
        }
        else
        {
            printf("\fTEST.DAT cannot\nbe found!");
        }
    }
}

Last updated: 2020/10/08