How to merge two files into third file in C

1 Answer

0 votes
#include <stdio.h>
   
void readFile(char file[]) {
    FILE *fp = fopen(file, "r");
       
	char ch;
    while ((ch = fgetc(fp)) != EOF)
        putchar(ch);

    fclose(fp);
}
    
void mergeFiles(char file1[], char file2[], char tofile[]) {
    FILE *fp1 = fopen(file1, "r");
    FILE *fp2 = fopen(file2, "r");
    FILE *tofp = fopen(tofile, "w");
     
    char ch;
    while ((ch = fgetc(fp1)) != EOF)
        fputc(ch, tofp);
     
    fputc('\n', tofp);
     
    while ((ch = fgetc(fp2)) != EOF)
        fputc(ch, tofp);
 
        
    fclose(fp1);
    fclose(fp2);
    fclose(tofp);
}
    
int main()
{
    char file1[100] = "d:\\data1.txt";
    char file2[100] = "d:\\data2.txt";
    char tofile[100] = "d:\\data_copy.txt";
    
    mergeFiles(file1, file2, tofile);
    readFile(tofile);
    
    return 0;
}
     
       
       
       
/*
run:
       
c c++ c#
java python
php javascript
nodejs
    
*/

 

 



answered Jul 8, 2020 by avibootz

Related questions

2 answers 198 views
1 answer 158 views
1 answer 127 views
2 answers 125 views
1 answer 142 views
...