程序代码如下:
#include
#include
int main() {
FILE * a;
int count = 0,ch;
a = fopen("c:\\text1.txt","rt");//打开文件之前,首先在C盘创建此文件text1.txt rt的意思是只读文本
if (a == NULL ) {
puts("Fail to open file!");
exit(0);
}
while(1) { //循环遍历文本中的字符,因为一次只读写文件中的一个字符
ch = fgetc(a);
if(ch == EOF)
break;
count++; //设定计数器,获取字符的个数
}
printf("%d",count);
fclose(a);//关闭文件
return 0;
}