john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file tail bytes

/* John Pfeiffer 2010-06 last # of bytes in a file */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{    
    char c;    
    FILE* fp;
    int bytes = 0;
    long size = 0;

    if( 3 != argc )
    {    printf("wrong number of arguments, to display the last # of bytes should be tail.exe filename 100\n");
        exit(1);
    }

    fp = fopen(argv[1],"rb");   /* raw read of binary */
    if( fp == NULL )
    {    printf("error opening file\n");    
    }
    else
    {
        bytes = atoi( argv[2] );

        if( fseek( fp , 0 , SEEK_END ) );
        {
            size = ftell( fp );            
            printf("%s has %ld bytes, starting from byte %ld\n\n", argv[1], size, size - (long)bytes );

            fseek( fp , size - (long)bytes , SEEK_SET );

        }

        c = getc(fp);
        while( !feof(fp) )
        {
            putc( c , stdout);
            c = getc(fp);
        }

        fclose(fp);
    }

    return( 0 );
}/* end main */

  • « programming basics configure make install
  • array pointer arithmetic strstr »

Published

Jun 12, 2010

Category

c

~102 words

Tags

  • bytes 1
  • c 95
  • file 92
  • tail 2