code.club

標題: 將二個文字檔案內容互換的程式 [打印本頁]

作者: enter    時間: 2015-3-10 21:51
標題: 將二個文字檔案內容互換的程式
#include <stdio.h>

int main(void){

char file1[10],file2[10],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;
}




歡迎光臨 code.club (https://code.club/) Powered by Discuz! X3.2