/* john pfeiffer 2009-11-28
outputs a file with the max # of chars desired before a newline
file-line-max-chars.c.exe filename.txt #
NEXT version will check if a word crosses the max and will put it on the next line
############################################################################### */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CMDLINE_PARAMETERS 3 /* parameter 1 is always the program name */
#define MAX_CHARS 640 /* max number of characters the program will accept on a line */
void test_commandline_parameters( int argc, char* argv[] );
void word_size( int max, char* buffer[] );
int main(int argc, char* argv[])
{
FILE* in_file_ptr;
FILE* out_file_ptr;
char c;
char output_filename[128];
/* char word[MAX_CHARS]; */
int max = 0,
/* word_size = 0; */
i = 0,
linecounter = 0,
lines_added = 0,
charcounter = 0;
test_commandline_parameters( argc, argv ); /* to ensure we have a filename */
in_file_ptr = fopen( argv[1], "r" ); /* source file */
if( in_file_ptr == NULL )
{ printf("Error opening input file!");
exit(1); }
max = atoi( argv[2] ); /* max # of chars desired per line */
if( (max < 0) || (max > MAX_CHARS) )
{ printf("Error, line size must be between 0 and 640.");
exit(1); }
sprintf( output_filename, "%d-", max );
strcat( output_filename, argv[1] );
out_file_ptr = fopen( output_filename, "w" ); /* output file */
if( in_file_ptr == NULL )
{ printf("Error opening input file!");
exit(1);
}
else{ printf("\nCreating %s file...",output_filename); }
do{
linecounter++; /* count the # of lines in the file */
charcounter = 0; /* reset the line character counter */
c = getc( in_file_ptr);
while( c != '\n' && c != EOF ) /* while not a newline or EOF */
{
/* fscanf( in_file_ptr, "%s", word); future version won't break words
word_size = strlen( word );
*/
charcounter++; /* increment count of chars on the line */
if( charcounter == max ) /* if the line is larger than the desired max */
{ putc( '\n', out_file_ptr ); /* insert a newline and reset the counter to 0 */
lines_added++;
charcounter=0;
}
putc( c, out_file_ptr );
c = getc( in_file_ptr); /* and continue reading the file */
}
if( c == '\n' )
{ putc( c, out_file_ptr );
charcounter = 0; /* if at the end of a line we must reset the counter */
}
}while( c != EOF); /*till the end of the file*/
fclose(in_file_ptr);
fclose(out_file_ptr);
printf("\n%d lines, %d lines added.\n",linecounter,lines_added);
return 0;
}/*end of main*/
/* ############################################################################### */
void test_commandline_parameters( int argc, char* argv[] )
{ int i = 0;
if( argc < CMDLINE_PARAMETERS )
{
printf("\nThis program will read a text file and output a file with at most max chars per line");
printf("\nMore parameters needed, e.g. %s infile.txt {integer of desired max line size}\n",argv[0]);
exit(1);
}
else if( argc > CMDLINE_PARAMETERS )
{ printf("\nToo many command line parameters, e.g. %s infile.txt {integer of desired max line size}\n");
exit(1);
}
else
{ while( i < argc )
{ printf("%s ", argv[i]);
i++;
}
printf("in progress...\n\n");
}
}/* end test_commandline_paramters func */