Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Merge contents of two files into a third file using C

#include <stdio.h>
#include <stdlib.h>
int main() {
	char file1[100];
	char file2[100];
	char file3[100];
	printf("Enter first file name: ");
	scanf("%s", file1);
	printf("Enter second file name: ");
	scanf("%s", file2);
	printf("Enter third file name: ");
	scanf("%s", file3);
	FILE *fp1 = fopen(file1, "r");
	FILE *fp2 = fopen(file2, "r");
	FILE *fp3 = fopen(file3, "w");
	char c;
	if (fp1 == NULL || fp2 == NULL || fp3 == NULL) {
		puts("Could not open files");
		exit(0);
   }
	while ((c = fgetc(fp1)) != EOF)
		fputc(c, fp3);
	while ((c = fgetc(fp2)) != EOF)
		fputc(c, fp3);
	printf("files are merged");
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.