這篇文章主要介紹了交換兩個文本內容的C語言代碼,有需要的朋友可以參考一下
文本存儲的位置:
jack.txt位於: e:jack.txt
retchie.txt位於: e:retchie.txt
內容:
jack.txt -> "Hello! I am Jack."
retchie.txt -> "Hello! I am Retchie."
相關代碼:
代碼如下:
#include <stdio.h>
int main(void)
{
char temp1[100];
char temp2[100];
FILE *p_jack;
FILE *p_retchie;
p_jack = fopen("e:/jack.txt", "r");
p_retchie = fopen("e:/retchie.txt", "r");
if (p_jack != NULL && p_retchie != NULL)
{
fgets(temp1, 20, p_jack);
fgets(temp2, 20, p_retchie);
}
fclose(p_jack);
fclose(p_retchie);
p_jack = fopen("e:/jack.txt", "w");
p_retchie = fopen("e:/retchie.txt", "w");
if (p_jack != NULL && p_retchie != NULL)
{
fprintf(p_jack, "%s", temp2);
fprintf(p_retchie, "%s", temp1);
fclose(p_jack);
fclose(p_retchie);
}
return 0;
}
運行結果:
內容:
jack.txt -> "Hello! I am Retchie."
retchie.txt -> "Hello! I am Jack."
感覺寫得太麻煩了.是否有更簡單的方法????