1
我有一個關於使用fgetc來計算指定文件中的字符的問題。 如何分別計算字符類型時如何使用它?例如,我只想計算僅包含小寫字符的數量,或者空格數量或標點符號等?有人可以舉個簡單的例子嗎?謝謝使用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;
}
您的if/else是語法錯誤,因爲'if'部分不在花括號中。請提供可編譯的C文件,最好使用縮進來反映代碼結構。 – Jens 2012-05-29 14:52:28