交换两个文本内容的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."
感觉写得太麻烦了.是否有更简单的方法????
相关文章
visual studio 将编译后的dll等文件自动复制到指定目录的方法
这篇文章主要介绍了visual studio 将编译后的dll等文件自动复制到指定目录,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧2024-03-03
最新评论