当前位置:网站首页>两个文件 合并为第三个文件 。
两个文件 合并为第三个文件 。
2022-06-26 19:11: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)//交替排列生成的串给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的内容如下:\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)//如果三个文件 中有一个打开失败,直接退出
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;
}边栏推荐
- Summary of alter operation in SQL
- Redis single sign on system + voting system
- 50行代码爬取Top500图书导入TXT文档
- ROS query topic specific content common instructions
- Crawl Douban to read top250 and import it into SqList database (or excel table)
- 为什么我不推荐去SAP培训机构参加培训?
- Soft test preparation multimedia system
- ARM裸板调试之串口打印及栈初步分析
- Database SQL statement writing
- Redis单点登陆系统+投票系统
猜你喜欢

Leetcode 128 longest continuous sequence

数据库SQL语句撰写

手机影像内卷几时休?

Leetcode interview question 29 clockwise print matrix

刷新三观的HP-UX系统中的强指针赋值出core问题

Clion compiling catkin_ WS (short for ROS workspace package) loads cmakelists Txt problems

项目实战六:分布式事务-Seata

Numpy's Matplotlib

Deep learning: numpy

Boot indicator monitoring
随机推荐
Installation and use of logstash
Summary of several common UML diagrams
Feign远程调用
Determine whether a sequence is a stack pop-up sequence
Logstash安装及使用
微信小程序 自定义 弹框组件
Union, intersection and difference operations in SQL
Summary of alter operation in SQL
To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
Basic and necessary common plug-ins of vscade
Installation and use of filebeat
ImageView, glide load long picture (glide load picture)
Project practice 4: user login and token access verification (reids+jwt)
知识点总结
项目实战四:用户登录及token访问验证(reids+jwt)
品达通用权限系统(Day 1~Day 2)
tsconfig. json
Request method 'POST' not supported
Project practice 6: distributed transaction Seata
Boot指标监测