我正在扫描文件并计算有多少个字母是大写,小写,数字或其他字符; 某种原因它给了我分段错误;我不太清楚为什么 这是我的代码
#include<stdio.h>
#include<ctype.h>
int main(void)
{
FILE *ifp, *ofp;
char *mode = "r";
char words;
int lengthOfWords, i;
int uppercase=0, lowercase=0, digits=0, other=0, total=0;
ifp = fopen("story.txt", "r");
if (ifp == NULL) {
fprintf(stderr, "Can't open input file in.list!\n");
return 0;
}
else
{
while(fscanf("%c", &words) != EOF)
{
if ((words>='A')&&(words<='Z'))
{
uppercase++;
}
else if ((words>='a')&&(words<='z'))
{
lowercase++;
}
else if ((words>='0')&&(words<='9'))
{
digits++;
}
else
{
other++;
}
}
}
printf("\n%d %d %d %d\n",uppercase, lowercase, digits, other );
return 0;
}
为什么我只是逐个字符地阅读它并按照它们计算它们
顺便说一句,这是我的txt文件。The quick Fox jumps
over 2014 *$!&#@] lazy dogs.
答案 0 :(得分:3)
您忘记将FILE
(流指针)作为fscanf
函数的参数传递:
while (fscanf(ifp, "%c", &words) != EOF)
根据man,fscanf
的签名是:
int fscanf(FILE *restrict stream, const char *restrict format, ...);