/* Trying to read througha text file that is- * n't wrapped so that the words are on the l- * ine instead of spread out really sucks f- * or me. This is a little project to wrap lines * so that words don't carry over to other lines * whilst reading through them with vi and * the like. * -Derrick Klise, 08/03/00 */ /* suggestion by XiRho: for(int i=0; (c = fgetc(foo)); !(i++%MAX) ? printf('\n') : printf("%c", c)); adapted: for(int i=0; (c = fgetc(foo)) != EOF; !(i++%MAX) ? printf('\n') : printf("%c", c)); suggestion by aka_arthur: int i; for (i=0; (c=getchar() != EOF ); i++) { /* stuff */ } */ #include #include #include #define MAX_LENGTH 60 main(int argc, char *argv[]) { FILE *infile; char line[MAX_LENGTH], inputchar; int counter; if (argc <= 1) { printf("textwrap v3.02 - Copyright Derrick Klise 2000\n"); printf("usage: %s inputfile\n", argv[0]); exit(EXIT_SUCCESS); } if ((infile = fopen(argv[1], "r")) == NULL) printf("couldn't open %s", argv[1]); else { do { for (counter = 0; counter <= MAX_LENGTH; counter++) { inputchar = getc(infile); printf("%c", inputchar); if (inputchar == '\n') { counter = 0; printf("\n"); } if (inputchar == EOF) exit(EXIT_SUCCESS); } do { inputchar = getc(infile); if (inputchar == EOF) exit(EXIT_SUCCESS); printf("%c", inputchar); } while (!isspace(inputchar)); printf("\n"); } while (inputchar != EOF); /* while EOF is not found in line */ fclose(infile); } return 0; }