C语言-将两个文件中的字符串拼接在一起写入第三个文件中

发布时间 2023-10-17 15:11:15作者: Bonne_chance

C语言-将两个文件中的字符串拼接在一起写入第三个文件中

步骤1. 先创建两个文件A.txt和B.txt,分别写入123, 456

步骤2. 编写程序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//将A.txt和B.txt中的字符串拼接,放入C.txt中
int main(){
	FILE *fa, *fb, *fc;
	int i, j, k;
	char str[100], str1[100];
	char tem;
	if((fa=fopen("A.txt", "r")) == NULL){
		printf("error: cannot open A file!\n");
		exit(0);
	}
	fgets(str, 99, fa);//从fa指向的地址取字符串,遇到换行则停止
	fclose(fa);
	
	if((fb=fopen("B.txt", "r")) == NULL){
		printf("error: cannot open B file!\n");
		exit(0);
	}
	fgets(str1, 99, fb);
	fclose(fb);
	strcat(str, str1);//字符串拼接

	if((fc=fopen("C.txt", "w"))==NULL){
		printf("error:cannot open C file!\n");
		exit(0);
	}
	fputs(str,fc);//将str写入fc指向的地址
	fclose(fc);
	system("pause");
	return 0;
	
	
	}

打开C.txt