此代碼包含 3 個檔案處理相關函式,它們從名為“mno”的檔案中讀取。但只有 main() 中的第一個呼叫函式在作業。如果串列的第一個函式被注釋,那么只有第二個函式可以作業,第三個函式不會。第三個也是一樣
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
void countVowel(char fin[])
{
FILE *fl;
char ch;
int count = 0;
fl = fopen(fin, "r");
while (ch != EOF)
{
ch = tolower(fgetc(fl));
count = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ? 1 : 0;
}
fclose(fl);
printf("Number of Vowels in the file \" %s \"-> \t %d \n", fin, count);
}
void countConsonant(char fin[])
{
FILE *fl;
char ch;
int count = 0;
fl = fopen(fin, "r");
while (ch != EOF)
{
ch = tolower(fgetc(fl));
count = (!(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') && (ch >= 'a' && ch <= 'z')) ? 1 : 0;
}
fclose(fl);
printf("Number of Consonant in the file \" %s \"-> \t %d \n", fin, count);
}
void countAlphabet(char fin[])
{
FILE *fl;
char ch;
int count = 0;
fl = fopen(fin, "r");
while (ch != EOF)
{
ch = tolower(fgetc(fl));
count = (ch >= 'a' && ch <= 'z') ? 1 : 0;
}
fclose(fl);
printf("Number of Alphabets in the file \" %s \"-> \t %d \n", fin, count);
}
int main()
{
countVowel("mno"); // output -> 10
countConsonant("mno"); // output -> 0
countAlphabet("mno"); // output -> 0
return 0;
}
這是“mno”檔案的內容->
qwertyuiopasdfghjklzxcvbnm, QWERTYUIOPASDFGHJKLZXCVBNM, 1234567890
uj5u.com熱心網友回復:
正如其他人所提到的,您的處理EOF不正確:
ch在第一次回圈迭代中未初始化- 做
tolower(fgetc(fl))會抹殺EOF價值。 - 使用
char ch;而不是int ch;允許將 [合法] 0xFF 視為 EOF。
但是,使用三個單獨的函式來創建三個不同的計數似乎很浪費,因為大部分時間花在 I/O 上,而不是確定我們正在查看的字符型別。當計數如此相關時尤其如此。
我們可以使用struct.
這是一個重構版本,它通過檔案一次計算所有三個計數:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
struct counts {
int vowels;
int consonants;
int alpha;
};
void
countAll(const char *fin,struct counts *count)
{
FILE *fl;
int ch;
int vowel;
count->vowels = 0;
count->consonants = 0;
count->alpha = 0;
fl = fopen(fin, "r");
if (fl == NULL) {
perror(fin);
exit(1);
}
while (1) {
ch = fgetc(fl);
// stop on EOF
if (ch == EOF)
break;
// we only care about alphabetic chars
if (! isalpha(ch))
continue;
// got one more ...
count->alpha = 1;
ch = tolower(ch);
// is current character a vowel?
vowel = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
// since we know it's alphabetic, it _must_ be either a vowel or a
// consonant
if (vowel)
count->vowels = 1;
else
count->consonants = 1;
}
fclose(fl);
printf("In the file: \"%s\"\n",fin);
printf(" Number of Vowels: %d\n",count->vowels);
printf(" Number of Consonants: %d\n",count->consonants);
printf(" Number of Alphabetics: %d\n",count->alpha);
}
int
main(void)
{
struct counts count;
countAll("mno",&count);
return 0;
}
對于給定的輸入檔案,程式輸出為:
In the file: "mno"
Number of Vowels: 10
Number of Consonants: 42
Number of Alphabetics: 52
uj5u.com熱心網友回復:
您正在使用ch未初始化的。在while (ch != EOF). 第一個函式呼叫之后的每個函式呼叫在開始時都ch等于 0,因為您忘記初始化它并且之前將記憶體設定為 -1。您可以通過替換這樣的回圈來修復它:
int ch;
...
while ((ch = fgetc(fl)) != EOF)
{
ch = tolower(ch);
count = ...;
}
這ch是在您檢查它之前進行初始化,然后轉換為小寫。
編輯:
請注意,這只適用ch于 int,因此它可以處理 -1 (EOF) 的值,并且位元組 255 不會被截斷為 -1。
編輯:
一開始我說ch一直都是0。是-1。很抱歉,我把它換成了空終止符,這通常是這種行為的原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523653.html
標籤:C文件处理fgetc
