/* john pfeiffer 05jun09
outputs lines from a given file (newline delimited) if they include the keyword (param 2)
TODO: accepts quotation marks as delimiter for a longer string (spaces & case sensitive)
file-number-of-lines-containing-keyword.c.exe filename.txt keyword
e.g. extractor.exe testfile.txt return > output.txt
(note the redirect > allows the end user more flexibility)
*/
#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 1024 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 */