/* capitalize the contents of infile and append to the infile by creating a temporary file */ #include #include #include main(int argc, char **argv) { int c; FILE *fp, *tmp_fp; FILE *gfopen(char *file_name, char *mode); if (argc != 2) { fprintf(stderr, "\n%s%s%s\n\n%s\n\n", "Usage: ", argv[0], " file-name", "The file will be doubled and some letters capitalized."); exit(1); } fp = fopen(argv[1], "r+"); tmp_fp = tmpfile(); /* tmpfile is defined in stdlib.h */ while ((c = getc(fp)) != EOF) putc(toupper(c), tmp_fp); rewind(tmp_fp); fprintf(fp, "--\n"); while ((c = getc(tmp_fp)) != EOF) putc(c, fp); } FILE *gfopen(char *file_name, char *mode) { FILE *fp; if ((fp = fopen(file_name, mode)) == NULL) { fprintf(stderr, "Cannot open %s - bye!\n", file_name); exit(1); } return fp; }