john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file extractor

/* john pfeiffer 05jun09

outputs lines from a given file (newline delimited) if they are included as a parameter

TODO: accepts quotation marks as delimiter for a string (case sensitive)

e.g. extractor testfile.txt return

would return all lines, probably best used in a batch file and or redirected e.g.

test.bat
testfile.txt %1 %2 > output.txt

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CMDLINE_PARAMETERS 3   /* The first parameter is always the program name */
#define LINEBUFFERMAX 1024      /* maximum of 512 characters before a newline */

void test_commandline_parameters( int argc, char* argv[] );

int main(int argc, char* argv[]){

    FILE* fptr;

    char linebuffer[LINEBUFFERMAX]; 
    int linecounter = 0,
         keywordcounter=0;


    char filename[256];
    char keyword[256];

    test_commandline_parameters( argc, argv );  /* to ensure we have a filename */

    strcpy(filename,argv[1]);
    strcpy(keyword,argv[2]);

    fptr = fopen(argv[1], "r");

    if(fptr == NULL)
    {   printf("error opening input file!");
        exit(1);        }

    while( ! feof(fptr) )                           /* till the end of the file */
    {   
        linecounter++;  
        fgets( linebuffer, LINEBUFFERMAX, fptr);    /* put a line into a char buffer */

        if( strstr( linebuffer, keyword ) != NULL )
        {   keywordcounter++;
            printf("%d ",linecounter);
            puts(linebuffer);
        }
    }

    fclose(fptr);

    printf("lines:%d \n",linecounter);
    printf("lines with keywords:%d",keywordcounter);

    return 0;
}/*end of main*/

void test_commandline_parameters( int argc, char* argv[] )
{   int i = 0;
    if( argc < CMDLINE_PARAMETERS )
    {   printf("\nMore parameters needed, e.g. %s infile.txt keyword\n",argv[0]);   
        exit(1);
    }
    else if( argc > CMDLINE_PARAMETERS )
    {   printf("\nToo many command line parameters, e.g.\n");   
        exit(1);
    }
    else
    {   while( i < argc )
        {   printf("%s ", argv[i]);     
            i++;
        }
        printf("in progress...\n\n");
    }   
}/* end test_commandline_paramters func */

  • « Sql query to get filesizes from a MOSS wss database
  • file count max line characters »

Published

Jun 5, 2009

Category

c

~211 words

Tags

  • c 95
  • extractor 1
  • file 92