当前位置:网站首页>The two files are merged into a third file.
The two files are merged into a third file.
2022-06-26 19:27:00 【laocooon】
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAX 1024
#define F1 "f1.txt"
#define F2 "f2.txt"
#define F3 "f3.txt"
void strcat(char* s1, char *s2,char *s3)// Alternate arrangement of generated strings s3
{
int i = 0,j=0,k=0;
while (s1[i] && s2[j])
{
s3[k++] = s1[i++];
s3[k++] = s2[j++];
}
while (s1[i])
{
s3[k++] = s1[i++];
}
while (s2[j])
{
s3[k++] = s2[j++];
}
s3[k] = '\0';
}
void printfile(char *filename)
{
char s[1024];
FILE* fp;
fp = fopen(filename, "r");
if (fp == NULL)
return;
printf("%s Is as follows :\n",filename);
while (fscanf(fp, "%s", s) != EOF)
{
printf("%s\n",s);
}
fclose(fp);
}
void merge(char *file1, char* file2, char* file3)
{
char s1[1024], s2[1024], s3[2048];
int tmp1,tmp2;
FILE* f1, * f2, * f3;
f1 = fopen(file1, "r");
f2 = fopen(file2, "r");
f3 = fopen(file3, "w");
if (f1 == NULL || f2 == NULL || f3 == NULL)// If three files One of the failed to open , immediate withdrawal
return ;
while (1)
{
tmp1 = fscanf(f1, "%s", s1);
tmp2 = fscanf(f2, "%s", s2);
if (tmp1 == EOF && tmp2 == EOF)
break;
else if (tmp1 != EOF && tmp2 != EOF)
{
strcat(s1, s2, s3);
fprintf(f3, "%s\n", s3); }
else if (tmp2 != EOF)
{
printf(f3, "%s\n", s2);
}
else if (tmp1 != EOF)
{
fprintf(f3, "%s\n", s1);
}
}
fclose(f1);
fclose(f2);
fclose(f3);
}
int main()
{
printfile(F1);
printfile(F2);
merge(F1,F2,F3);
printfile(F3);
return 0;
}
边栏推荐
猜你喜欢
Project practice 4: user login and token access verification (reids+jwt)
Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems
Redis single sign on system + voting system
抖音实战~分享模块~复制短视频链接
微服务版单点登陆系统(SSO)
Filebeat安装及使用
Web resource preloading - production environment practice
关于不等式取值转义的思路
Microservice architecture
Convex hull problem
随机推荐
Tiktok practice ~ sharing module ~ copy short video link
链游开发成品源码 链游系统开发详情说明
黑客用机器学习发动攻击的九种方法
Project practice 5: build elk log collection system
转:实事求是
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
(几何) 凸包问题
Tiktok practice ~ search page ~ scan QR code
When does the mobile phone video roll off?
wm_ Concat() and group_ Concat() function
【Mysql系列】工作常用sql集锦(持续更新)
tsconfig. json
Some basic mistakes
字符串String转换为jsonArray并解析
项目实战五:搭建ELk日志收集系统
Boot indicator monitoring
Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用
限流设计及实现
Feign remote call
为什么我不推荐去SAP培训机构参加培训?