69,371
社区成员
发帖
与我相关
我的任务
分享
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
char str[10]={};
FILE *fp,*fp2;
if((fp=fopen("test.txt","r"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
if((fp2=fopen("test2.txt","w"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
while(!feof(fp))//设置断点的行
fputc(fgetc(fp),fp2);
fgets(str,3,fp);//出问题的地方
printf("%s",str);
fclose(fp);
fclose(fp2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[10] = {0};
FILE *fp,*fp2;
if((fp=fopen("test.txt","r"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
if((fp2=fopen("test2.txt","w"))==NULL)
{
printf("cannot open this file.\n");
exit(0);
}
while(!feof(fp))//设置断点的行
fputc(fgetc(fp),fp2);
rewind(fp); /* or fseek(fp, 0l, SEEK_SET);*/
fgets(str,3,fp);//出问题的地方
printf("%s",str);
fclose(fp);
fclose(fp2);
return 0;
}
改一下fp的位置,使用rewind或fseek