int count;
char c;
FILE *fp;
fp=fopen("number.txt","r");
if(fp==NULL)
{
printf("Can't open the file!\n");
exit(0);
}
while(!feof(fp))
{
count=0;
while ((c=fgetc(fp))!=EOF)
{
if (c < 0)
count++;
}
}
fclose(fp);
printf("%d\n", count / 2);
c<0的判断并不靠谱,中文是两个字节,其中首字节肯定大于0x80,但第二个字节不一定小于0x80啊
你应该判断前一个字节小于0时,跳过下一个字节
int s = 0;
while ((c=fgetc(fp))!=EOF)
{
if(s == 1)
{
s = 0;
continue;
}
count++;
if(c < 0)
s = 1;
}
看看你的txt里是否有不可见字符,看看汉字后面是否能用鼠标选中,如果能选中,说明有不可见字符。
是在不行,就把c都打印一下看看多了什么东西
if(c<0)
{
printf("%d ",c);
count++;
}
printf("\n");