#include #include #include #include int main(int argc, char **argv) { if (argc != 2) { printf("Usage: %s filename\n", strrchr(argv[0], '/')+1); exit(0); } int hash[128] = {0}; int hash_letter[256] = {0}; FILE *fd = fopen(argv[1], "r+"); if (fd == NULL) { printf("err: %s\n", strerror(errno)); goto err; } char buf = 0; int letter_len = 0; int segment_num = 0; char newline = '\n'; while (fread(&buf, 1, 1, fd)>0) { hash[(int)buf]++; printf("%c", buf); if (((buf >= 'A') && (buf <= 'Z')) || ((buf >= 'a') && (buf <= 'z'))) { letter_len++; newline = ' '; } else { if (buf == '\n') { if (newline != '\n') { segment_num++; } newline = '\n'; } if (letter_len > 0) { hash_letter[letter_len]++; letter_len = 0; } } } printf("File information:\n"); printf("Tab num: %d\n", hash[9]); printf("Newline num: %d\n", hash[10]); printf("Space num: %d\n", hash[32]); int i; for (i=33; i<126; i++) { if (hash[i] != 0) { printf("%c num: %d\n", (char)i, hash[i]); } } for (i=0; i<256; i++) { if (hash_letter[i] != 0) { printf("%d length letters num: %d\n", i, hash_letter[i]); } } printf("segment num is: %d\n", segment_num); fclose(fd); return 0; err: return -1; }