/* john pfeiffer 05jun09
returns a count the max # of chars before a newline in a text file
file-count-max-line-characters.c.exe filename.txt
############################################################################### */
#include <stdio.h>
#include <stdlib.h>
#define CMDLINE_PARAMETERS 2 /* The first parameter is always the program name */
void test_commandline_parameters( int argc, char* argv[] );
int main(int argc, char* argv[]){
FILE* fptr;
char c;
int max = 0,
last_max_linenumber =0,
linecounter = 0,
charcounter = 0;
test_commandline_parameters( argc, argv ); /* to ensure we have a filename */
fptr = fopen(argv[1], "r");
if(fptr == NULL)
{ printf("error opening input file!");
exit(1); }
do{
linecounter++; /* count the # of lines in the file */
charcounter = 0; /* reset the line character counter */
c = getc(fptr);
while(c != '\n' && c != EOF) /* while not a newline or EOF */
{
charcounter++;
c = getc(fptr); /*and continue writing to file*/
}
if( max < charcounter ) /* found a new max character line */
{ max = charcounter;
last_max_linenumber = linecounter;
}
charcounter = 0;
}while( c != EOF); /*till the end of the file*/
fclose(fptr);
printf("%d lines\n%d was the longest line with %d chars\n",linecounter, last_max_linenumber, max);
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\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 */