distributedlife

passionate about everything

Vanilla C with Sprinkles – file existence and content checking file existence and whether a file is empty using Vanilla C Published by Ryan Boucher @ 11:55 pm

Two useful little functions the first is file exists which is self explanatory.


enum EnabledOptions FileExists (const char* const Filename)
{
    long FileHandle = 0 ;

    if (FileHandle = fopen (Filename, "r"))
    {
        fclose (FileHandle) ;
        return Yes ;
    }

    return No ;
}

The second function is called File is Empty. Both of these functions form more complicate processes used for querying the registry and event logs using Vanilla C.



enum EnabledOptions FileIsEmpty (const char* const Filename)
{
    const unsigned int SEEK_END = 2 ;
    long FileHandle = 0 ;
    enum EnabledOptions ReturnValue = Yes; 

    FileHandle = fopen (Filename, "rb" );
    if (!FileHandle)
    {
        lr_error_message ("Failed to open the file \"%s\"", Filename) ;

        return Yes;
    }

    fseek (FileHandle , 0 , SEEK_END);
    if (ftell (FileHandle) != 0)
    {
        ReturnValue = No ;
    }

    fclose (FileHandle) ;

    return ReturnValue ;
}
My Mug Ryan Boucher is a Software Inquisitor and is passionate about it. You can find a whole raft of articles and anecdotes about software testing and other topics he gets excited about.
Tags , , , , , ,