rewind 为什么没有起到作用呢?还有一个关于fclose的问题
程序代码:#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *out,*fp;
char c;
if((fp=fopen("test.txt","w"))==NULL)
{
printf("can't open the file.\n");
exit(0);
}
while((c=getchar())!='#')
fputc(c,fp);
rewind(fp);
if((out=fopen("out.txt","w"))==NULL)
{
printf("can't open the file.\n");
exit(0);
}
while((c=fgetc(fp))!=EOF)
fputc(c,out);
fclose(fp);
fclose(out);
}
这里 rewind 为什么没有起到作用呢?读取的out.txt是空文件。
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
FILE *fp;
char a[][20]={"apple","grade","pear"},strout[20]="";
int i;
if((fp=fopen("f.txt","w"))==NULL)
{
printf("File open error!\n");
exit(-1);
}
for(i=0;i<3;i++)
fputs(a[i],fp);
fclose(fp);//为什么这里要关闭一次 才可以?如果不关闭 直接用rewind就会出现我上面那个问题
if((fp=fopen("f.txt","r"))==NULL)
{
printf("File open error.\n");
exit(-1);
}
i=0;
while(!feof(fp))
{
if(fgets(strout,strlen(a[i++])+1,fp)!=NULL)
puts(strout);
}
fclose(fp);
return 0;
}请大家帮帮忙 谢谢了






