#include <stdio.h>
#include <ctype.h>
void readFile(FILE *file);
void count (FILE *file, int *alpha, int *digit, int *punct, int *spaces);
void printMessage(int *alpha, int *digit, int *punct, int *spaces);
int main( void ) {
FILE *file = NULL;
readFile(file);
return 0;
}
void readFile(FILE *file) {
file = fopen("/Users/AdmiralDanny/Desktop/testFile.txt", "r");
int *alpha = NULL, *digit = NULL, *punct = NULL, *space = NULL;
if (file) {
count(file, alpha, digit, punct, space);
fclose(file);
} else {
perror( "error opening the file" );
}
}
void count (FILE *file, int *alpha, int *digit, int *punct, int *spaces) {
int ch;
while ((ch = fgetc(file)) != EOF ) {
if (isalpha(ch) != 0) {
++alpha;
} else if (isdigit(ch)) {
++digit;
} else if (ispunct(ch)) {
++punct;
} else if (isspace(ch)) {
++spaces;
}
}
printMessage(alpha, digit, punct, spaces);
}
void printMessage(int *alpha, int *digit, int *punct, int *spaces) {
printf( "alphabetic characters: %d\n", alpha );
printf( "digit characters: %d\n", digit);
printf( "punctuation characters: %d\n", punct );
printf( "whitespace characters: %d\n", spaces );
}
我的 .txt 文件只有 4 个空格和 12 个字母字符,但它给了我 48 个字母字符和 8 个空格字符?为什么会这样?另外,当我尝试在 printMessage 方法中打印出 int 指针时,我会收到警告