我有一个关于使用 fgetc 计算指定文件中的字符的问题。当您必须单独计算字符类型时,您如何使用它?例如,我只想计算小写字符的数量,或者空格的数量,或者标点符号等?有人可以举一个简短的例子吗?谢谢
我试着做这个程序,希望能计算字符总数,你如何挤入单独字符类型的数量?我不确定这个程序是否正确
#include <stdio.h>
int main (void)
{
//Local declarations
int a;
int count = 0;
FILE* fp;
//Statements
if (!(fp = fopen("piFile.c", "r")))
{
printf("Error opening file.\n");
return (1);
}//if open error
while ((a = fgetc (fp)) != EOF)
{
if (a != '\n')
count++;
printf("Number of characters: %d \n", count);
else
printf("There are no characters to count.\n");
}
fclose(fp);
return 0;
}