C examples for File:File Read
Count character in a file
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char character; int ch;//from w w w . j a va 2 s . co m character = 'c'; FILE * fp; int counts[3]; for (int i = 2; i < argc; i++) { counts[i - 2] = 0; if ((fp = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "Can't open file %s\n", argv[i]); continue; } while ((ch = getc(fp)) != EOF) if (ch == character) counts[i - 2]++; fclose(fp); } printf("Character count for %c in ...\n", character); for (int i = 2; i < argc; i++) printf("%s: \t\t%d\n", argv[i], counts[i - 2]); return 0; }