/* file19 reads a list of file names, one name per line, and prints the number of characters in each file. The last line of input is required to end with a newline. Some things to try: Run file18 | file19. Change this program to check for errors on stdin. (It might be reading from a disk file, and the disk might die.) Change this program to handle larger lengths; see file16.c. Change this program to use a dynamically allocated file name. */ #include #include #include void die_read(char *filename) { fprintf(stderr,"fatal: unable to read %s: %s\n",filename,strerror(errno)); exit(111); } void printlength(char *filename) { unsigned int bytes = 0; FILE *f = fopen(filename,"r"); char c; if (!f) die_read(filename); while (fscanf(f,"%c",&c) == 1) ++bytes; if (ferror(f)) die_read(filename); fclose(f); printf("%s has %u bytes.\n",filename,bytes); } int main(void) { char line[1024]; int linelen = 0; char c; while (scanf("%c",&c) == 1) { if (linelen >= sizeof line) { fprintf(stderr,"fatal: filename too long\n"); exit(111); } if (c == '\n') { line[linelen] = 0; printlength(line); linelen = 0; } else line[linelen++] = c; } return 0; }