C Language Development Kit/Functions/Fsize

From OptiWiki

Jump to: navigation, search
Description This function returns the size of a file, measured in bytes.
Syntax
long fsize(char * filename);
Arguments
char * filename
Points to a string containing the name of the file whose size is to be determined.
Returns The size of the file, measured in bytes.

When the file cannot be found, -1 is returned.

Remarks On the OPH100x, the default drive will be used. Use the function set_default_drive to select that drive.
Example
#include <stdio.h>
#include <stdlib.h>
#include "lib.h"
 
void main( void )
{
    long size;
    FILE *fp;
    static char data[20+1];
    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");
 
    while( 1 )
    {
        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!");
        }
    }
}


Views
Personal tools