交换两个文本内容的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."
感觉写得太麻烦了.是否有更简单的方法????