求教一个问题 感激不尽 我是小白 请问如何在C++中判断txt文件末尾是否为空???

2025-01-01 19:11:45
推荐回答(1个)
回答1:

#include
#include
#include
void main()
{
FILE*fp;
char s[100],i=0,ch;
if((fp=fopen("d:\\Test.txt","w"))==NULL)
{
printf("cannot open\n");
exit(0);
}
while((ch=getchar())!='!')
{
s[i]=ch;
if('a'<=s[i]&&s[i]<='z')
s[i]-=32;
i++;
}
s[i]='\0';
fputs(s,fp);
fclose(fp);
if((fp=fopen("d:\\Test.txt","r"))==NULL)
{
printf("cannot open\n");
exit(0);
}
i=0;
while(!feof(fp))
{
fscanf(fp,"%c",s+i);
i++;
}
s[i]='\0';
fclose(fp);
puts(s);

}
不知道是否满足你的要求!?!