统计在读到文件结尾前读取的字符数
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 0;//整形变量i用来计数
int ch;//定义一个字符来和文件结尾做比较
FILE*fp;
char fname[50];
printf("Enter the name of the file:");
scanf("%s", fname);//输入文件名,需要文件路径
fp = fopen(fname, "r");//以只读形式打开文件
if (fp == NULL) //如果需要打开的文件不存在,报错,退出程序
{
printf("Failed to open file.Bye\n");
system("pause");
exit(1);
}
while ((ch=getc(fp))!=EOF)//判断是否读取到文件结尾
{
i++;//每读取一个字符,i自加一次
}
fclose(fp);//关闭打开的文件
printf("the file have %d byte", i);//输出文件有多少字符
system("pause");//程序结束之前稍微等待
}
***yzxie处女博***