#include #include enum { ERROR = -1, SUCCESS = 0, ASCII_Line_feed = 10, ASCII_Carriage_return = 13, ASCII_Record_Separator = 30, MAX_LINE_SIZE = 43162 //fails but 43161 works just fine ??? }; struct _node { int value; struct _node *next; }; typedef struct _node node; int main(int argc, char *argv[]) { int c; int count; node *list = (node *)malloc(sizeof(node)); list->next = NULL; list = NULL; FILE *fin; /* input file */ FILE *fout; /* output file */ /* write a single character to the output file */ void write_text(node *line) { fputc (line->value, fout); /* Deal with case of not end of line */ if(line->next != NULL) { write_text(line->next); line->next = NULL; } /* free allocated space that is no longer being used */ //free(line); line = NULL; return; } /* Write entire line to file */ void write_line() { /* Start line with the length of the line */ fputc (count / 256, fout); fputc (count % 256, fout); count = 0; /* Deal with case of line without any text */ if(list != NULL) { write_text(list); } /* Append end of line characters */ fputc (ASCII_Carriage_return, fout); fputc (ASCII_Line_feed, fout); list = NULL; return; } /* add character to line or in the event that the line has reached maxed size write line */ void add_node(node *a) { /* max line size check and correction */ if(count == MAX_LINE_SIZE) { write_line(); } /* deal with case of first character on line */ if(a == NULL) { list = (node *)malloc(sizeof(node)); list->value = c; list->next = NULL; return; } /* deal with inserting next character on line */ if(a->next == NULL) { node *b = (node *)malloc(sizeof(node)); a->next = b; b->value = c; b->next = NULL; return; } /* Walks down the line until the next character is a NULL */ add_node(a->next); return; } /* only allow a correct number of inputs */ if(argc != 3) { printf("ERROR: improper input, please format as : \ntxt2text input_file_name output_file_name"); return ERROR; } /* open input file */ fin = fopen(argv[1], "r"); if (fin==NULL) { printf("\nUnable to open file: %s!\n", argv[1]); return ERROR; } /* open output file */ fout = fopen(argv[2], "w"); if (fout==NULL) { printf("\nUnable to open output file: %s!\n", argv[2]); return ERROR; } /* Initialize local variables */ count = 0; /* main loop */ do { c = getc(fin); switch (c) { case ASCII_Line_feed : { c = getc(fin); if (c != ASCII_Carriage_return) { ungetc(c, fin); } } case ASCII_Carriage_return : { c = getc(fin); if (c != ASCII_Line_feed) { ungetc(c, fin); } } case ASCII_Record_Separator : case EOF : write_line(); break; default : count = count + 1; add_node(list); } }while (c != EOF); /* close input file */ fclose(fin); /* close output file */ fclose(fout); /* sinal success and terminate */ return SUCCESS; }