enter 發表於 2015-3-10 21:51:33

將二個文字檔案內容互換的程式

#include <stdio.h>

int main(void){

char file1,file2,ch,*p;
FILE *fp1,*fp2,*temp;
/*
printf("file1:\n");
gets(file1);
printf("file2:\n");
gets(file2);
*/

printf("enter 2 files to switch the contents:\n");
scanf("%s %s",file1,file2);

fp1=fopen(file1,"rb");
temp=fopen("temp.txt","w+b");

for(;;)
{
ch=fgetc(fp1);
if(ch==EOF) break;
fputc(ch,temp);
}

fclose(fp1);
fclose(temp);

fp2=fopen(file2,"rb");
fp1=fopen(file1,"wb");

for(;;)
{
ch=fgetc(fp2);
if(ch==EOF) break;
fputc(ch,fp1);
}

fclose(fp1);
fclose(fp2);

temp=fopen("temp.txt","rb");
fp2=fopen(file2,"wb");

for(;;)
{
ch=fgetc(temp);
if(ch==EOF) break;
fputc(ch,fp2);
}

fclose(fp2);
fclose(temp);

return 0;
}
頁: [1]
查看完整版本: 將二個文字檔案內容互換的程式