以下是源代碼,實現的結果是,磁盤中的信息是隔一個字符復制的,不知道為什麼,求指點,謝謝。
#include
#include
#include
main()
{
FILE *fp;
FILE *fp1;
char ch;
char filename[10];
char filename1[10];
char filepath[50];
char filepath1[100];
printf("give a file a filename: ");
scanf("%s",filename);
sprintf(filepath,"/socketTest/photo/%s",filename);
if((fp=fopen(filepath,"w+"))==NULL)
{
printf("cannot open the file <%s>\n",filename);
return 0;
}//if
ch=getchar();
printf("please input:\n");
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}//while
fclose(fp);
printf("give another file a filename: ");
scanf("%s",filename1);
ch=getchar();
sprintf(filepath1,"/socketTest/photo/%s",filename1);
if((fp1=fopen(filepath1,"w+"))==NULL)
{
printf("cannot open the file <%s>\n",filename1);
return 0;
}//if
printf("the length of %s is %d\n",filename1,strlen(filepath1));
if((fp=fopen(filepath,"r"))==NULL)
{
printf("cannot open the file <%s>\n",filename);
return 0;
}//if
while(fgetc(fp)!=EOF)
{
printf("%c",fgetc(fp));
fputc(fgetc(fp),fp1);
}
fclose(fp1);
fclose(fp);
printf("\n file %s is ok!\n",filename1);
}//main
下面的代碼使得字符指針fp一共向前移動了3次,不跳反而不正常了。
while(fgetc(fp)!=EOF)
{
printf("%c",fgetc(fp));
fputc(fgetc(fp),fp1);
}
fgetc(fp)
調用了3次...
1樓正解.