C_primer_pluse_13——3——state

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define LEN 40
 5 int main(int argc, char const *argv[])
 6 {
 7     FILE *in, *out;
 8     char name[LEN];
 9     int ch = 0, count = 0;
10     if (argc < 2) { fprintf(stderr, "%s", argv[1]); exit(EXIT_FAILURE); }
11     if ((in = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s", argv[1]); exit(EXIT_FAILURE); }
12     strcpy(name, argv[1]);
13     name[LEN - 5] = '\0';
14     strcat(name, ".red");//in "r" out "w"
15     if ((out = fopen(name, "w")) == NULL) { fprintf(stderr, "%s", argv[1]); exit(EXIT_FAILURE); }
16     while ((ch = getc(in)) != EOF) { if (count++ % 3 == 0) { putc(ch, out); } }
17     if (fclose(in) != 0 && fclose(out) != 0) { fprintf(stderr, "%s", argv[1]); exit(EXIT_FAILURE); }
18     return 0;
19 }

internally 

  16 while ((ch = getc(in)) != EOF) { if (count++ % 3 == 0) { putc(ch, out); } } 

read in point onto argv[1]  tackle complete pass follow down 

 putc(ch, out);

inner "out" is putc function purpose output 

posted @ 2021-05-31 21:17  wcl王成龙  阅读(48)  评论(0)    收藏  举报