---9.1--- #include #include #include #include int main(int argc, char *argv[]) { FILE *f; int freq[26], c, i; assert(argc == 2); f = fopen(argv[1], "r"); if (f == NULL) { printf("ファイル%sがオープンできません.\n", argv[1]); return EXIT_FAILURE; } for (i = 0; i < 26; i++) freq[i] = 0; while ((c = fgetc(f)) != EOF) if (isalpha(c)) freq[tolower(c) - 'a']++; fclose(f); for (i = 0; i < 26; i++) printf("%c :%5d\n", i + 'a', freq[i]); return EXIT_SUCCESS; } ---9.2--- #include #include #include #include #define SIZE 16 main(int argc, char *argv[]) { void disp_a_line(char *s, int c_cnt); FILE *f; char s[SIZE]; int c, c_cnt, l_cnt; assert(argc == 2); f = fopen(argv[1], "rb"); assert(f != NULL); c_cnt = l_cnt = 0; while ((c = fgetc(f)) != EOF) { s[c_cnt++] = c; if (c_cnt == SIZE) { disp_a_line(s, c_cnt); if (++l_cnt == 16) { putchar('\n'); getchar(); l_cnt = 0; } c_cnt = 0; } } if (c_cnt) disp_a_line(s, c_cnt); fclose(f); return EXIT_SUCCESS; } void disp_a_line(char *s, int c_cnt) { static long count = 0; int i; printf("%8.8lX : ", count); count += c_cnt; for (i = 0; i < c_cnt; i++) printf("%2.2X ", s[i] & 0xFF); printf("%*s", 3 * (SIZE - i), ""); putchar(' '); for (i = 0; i < c_cnt; i++) putchar(isprint(s[i]) ? s[i] : '.'); putchar('\n'); } ---9.3--- #include #include #include #include #define SIZE 100 main(int argc, char *argv[]) { FILE *f, *fs, *fn; char buf[SIZE]; int l_cnt, len; if (argc != 2) { printf("ファイル名を一つ(だけ)指定して下さい.\n"); return EXIT_FAILURE; } strcpy(buf, argv[1]); len = strlen(buf); if (len < 4 || (strcmp(buf + len - 2, ".c") && strcmp(buf + len - 2, ".C"))) { printf("ファイル名が正しくありません.\n"); return EXIT_FAILURE; } f = fopen(argv[1], "r"); if (f == NULL) { printf("入力ファイルがオープンできません.\n"); return EXIT_FAILURE; } *(buf + len - 1) = 's'; fs = fopen(buf, "w"); *(buf + len - 1) = 'n'; fn = fopen(buf, "w"); if (fs == NULL || fn == NULL) { printf("出力ファイルがオープンできません.\n"); return EXIT_FAILURE; } l_cnt = 0; while (fgets(buf, SIZE, f) != NULL) fprintf(isspace(*buf) ? fs : fn, "%d:%s", ++l_cnt, buf); fclose(f); fclose(fs); fclose(fn); return EXIT_SUCCESS; } ---9.4--- #include #include #include #include #define SIZE 100 main(int argc, char *argv[]) { int readin(FILE *f, char *s, char **p); FILE *f, *fs, *fn; int l_cnt_s, l_cnt_n, len; char buf_s[SIZE + 10], buf_n[SIZE + 10], *ps, *pn; if (argc != 2) { printf("ファイル名を一つ(だけ)指定して下さい.\n"); return EXIT_FAILURE; } strcpy(buf_s, argv[1]); len = strlen(buf_s); if (len < 4 || (strcmp(buf_s + len - 2, ".c") && strcmp(buf_s + len - 2, ".C"))) { printf("ファイル名が正しくありません.\n"); return EXIT_FAILURE; } f = fopen(buf_s, "w"); if (f == NULL) { printf("出力ファイルがオープンできません.\n"); return EXIT_FAILURE; } *(buf_s + len - 1) = 's'; fs = fopen(buf_s, "r"); *(buf_s + len - 1) = 'n'; fn = fopen(buf_s, "r"); if (fs == NULL || fn == NULL) { printf("入力ファイルがオープンできません.\n"); return EXIT_FAILURE; } l_cnt_s = readin(fs, buf_s, &ps); l_cnt_n = readin(fn, buf_n, &pn); while (l_cnt_s && l_cnt_n) if (l_cnt_s < l_cnt_n) { fputs(ps, f); l_cnt_s = readin(fs, buf_s, &ps); } else { fputs(pn, f); l_cnt_n = readin(fn, buf_n, &pn); } while (l_cnt_s) { fputs(ps, f); l_cnt_s = readin(fs, buf_s, &ps); } while (l_cnt_n) { fputs(pn, f); l_cnt_n = readin(fn, buf_n, &pn); } fclose(f); fclose(fs); fclose(fn); return EXIT_SUCCESS; } int readin(FILE *f, char *s, char **p) { int n, c; char *t; if (fgets(s, SIZE + 8, f) == NULL) return 0; t = s; n = 0; while (c = *t, isdigit(c)) { n = 10 * n + (c - '0'); t++; } if (t == s || *t != ':') { /* 行の先頭が数字でないか,区切りのコロンがない */ printf("ファイルの内容が正しくありません.\n"); exit(EXIT_FAILURE); } *p = t + 1; /* 本来の行の先頭 */ return n; }