C语言文件问题
#include<stdio.h>#include<stdlib.h>
#include<stdbool.h>
#define LENGTH 80
int main()
{
char *proverbs[]=
{"Many a mickle makes a muckle.\n",
"Too many cooks spoil the broth.\n",
"He who laughs last didn't get the joke in"
"the first place.\n"
};
char more[LENGTH];
FILE *pfile=NULL;
char *filename="C:\\myfile.txt";
if(!(pfile=fopen(filename,"w")))
{
printf("Enter opening %s for writing.Program terminated.",filename);
exit(1);
}
int count=sizeof proverbs/sizeof proverbs[0]; 这句是什么意思,看不懂这句话在这里的用意。
for(int i=0;i<count;i++)
fputs(proverbs[i],pfile);
fclose(pfile);
if(!(pfile=fopen(filename,"a")))
{
printf("Enter opening %s for writing.Program terminated.",filename);
exit(1);
}
printf("Enter proverbs of less than 80 character or press Enter to end:\n");
while(true) 这句话在这里是不是有点多余了,上面的if语句不是已经判断过了吗?
{
fgets(more,LENGTH,stdin);
if(more[0]=='\n')
break;
fputs(more,pfile);
}
fcolse(pfile);
if(!(pfile=fopen(filename,"r")))
{
printf("Enter opening %s for writing.Program terminated.",filename);
exit(1);
}
printf("The proverbs in the file are:\n\n");
while(fgets(more,LENGTH,pfile))
printf("%s",more);
fclose(pfile);
remove(filename);
return 0;
}