#include <stdio.h>
#include<stdlib.h>
void main(void)
{
int ch;
FILE *fp;
unsigned long count=0;
char filename[50];
printf("enter the file name\n");
while((scanf("%s",filename))!=1)
{
printf("usage:%s filename\n",filename);
exit(EXIT_FAILURE);
}
if((fp=fopen(filename,"r"))==NULL)
{
printf("can't open %s filename\n",filename);
exit(EXIT_FAILURE);
}
while((ch=getc(fp)!=EOF))
{
putc(ch,stdout);
count++;
}
fclose(fp);
printf("file %s has %lu characters\n",filename,count);
}
我的文件只有一个字符f,为什么显示读了370个字符呢
https://www.cnblogs.com/kekea/p/4506560.html
我用你的程序试着打开了多个文本文件,结果都是正确的,你的程序没有什么问题,错误可能出现在你要读取的文件上,如果文件是二进制文件,你用文本编辑器打开时,有可能只看到一个字符,但实际文件并不是一个字节。
我这里没有MAC系统,所以不能还原你的测试,你可以试着将
while((ch=getc(fp)!=EOF))
{
putc(ch,stdout);
count++;
}
改成
while(!feof(fp))
{
ch = getc(fp);
if (ch != -1)
{
putc(ch,stdout);
count++;
}
}