統計data.txt文件中大寫字符、小寫字符、數字、其他字符的個數,代碼如下:
/*統計一個文件中的大寫字母 小寫 字母 數字 及其他字符的個數*/
void chapter1::statistic()
{
printf_s("統計文件開始\r\n");
char *path = "D:\\000testprocess\\c_lesson\\Debug\\data.txt";
FILE *f;
int r = fopen_s(&f, path, "r");
if (r>0)
{
printf_s("讀取文件錯誤");
}
int capletter = 0, lowercase = 0 , num = 0, other = 0;
char temp = fgetc(f);
while (!feof(f))
{
if (temp>='a' && temp <='z')
{
lowercase++;
}
else if (temp >= 'A' && temp <= 'Z')
{
capletter++;
}
else if (temp >='0' && temp <= '9')
{
num++;
}
else
{
other++;
}
temp = fgetc(f);
}
printf_s("大寫字符:%d 小寫字符個數:%d 數字個數:%d 其他字符個數:%d",capletter,lowercase,num,other);
fclose(f);
printf_s("統計文件結束\r\n");
}