C Program to Merge Contents of Two Files into a Third File

This C Program join lines of two given files and store them in a new file.

Here is source code of the C Program to join lines of two given files and store them in a new file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Join Lines of Two given Files and 
  3.  * Store them in a New file
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. /* Function Prototype */
  9. int joinfiles(FILE *, FILE *, FILE *);
  10.  
  11. char ch;
  12. int flag;
  13.  
  14. void main(int argc, char *argv[])
  15. {
  16.     FILE *file1, *file2, *target;
  17.  
  18.     file1 = fopen(argv[1], "r");
  19.     if (file1 == NULL)
  20.     {
  21.         perror("Error Occured!");
  22.     }
  23.     file2 = fopen(argv[2], "r");
  24.     if (file2 == NULL)
  25.     {
  26.         perror("Error Occured!");
  27.     }
  28.     target = fopen(argv[3], "a");
  29.     if (target == NULL)
  30.     {
  31.         perror("Error Occured!");
  32.     }
  33.  
  34.     joinfiles(file1, file2, target);        /* Calling Function */
  35.  
  36.     if (flag == 1)
  37.     {
  38.         printf("The files have been successfully concatenated\n");
  39.     }
  40. }
  41.  
  42. /* Code join the two given files line by line into a new file */
  43.  
  44. int joinfiles(FILE *file1, FILE *file2, FILE *target)
  45. {
  46.     while ((fgetc(file1) != EOF) || (fgetc(file2) != EOF))
  47.     {
  48.         fseek(file1, -1, 1);
  49.         while ((ch = fgetc(file1)) != '\n')
  50.         {
  51.             if (ch == EOF)
  52.             {
  53.                 break;
  54.             }
  55.             else
  56.             {
  57.                 fputc(ch, target);
  58.             }
  59.         }
  60.         while ((ch = fgetc(file2)) != '\n')
  61.         {
  62.             if (ch == EOF)
  63.             {
  64.                 break;
  65.             }
  66.             else
  67.             {
  68.                 fputc(ch, target);
  69.             }
  70.         }
  71.         fputc('\n', target);
  72.     }
  73.     fclose(file1);
  74.     fclose(file2);
  75.     fclose(target);
  76.     return flag = 1;
  77. }

$ cc file7.c
$ ./a.out lines.c words.c final.c
The files have been successfully concatenated
 
/* FIRST FILE */
 
/*
Hello!!
This is a C Program File.
Consider a code to Add two numbers 
*/
 
#include <stdio.h>
/* Function Prototype */
int sum(int,  int);
void main()
{
    int num1, num2;
    printf("Enter Number1 and Number2:");
    scanf("%d %d ", num1, num2);
    sum(num1, num2);
}
 
int sum(int a, int b)
{
    return a + b;
}
 
/* SECOND FILE */
 
/*
 * this is temporary file for use in file handling
 */
#include <stdio.h>
 
int sqrt(int);
void main()
{
    int num;
    printf("enter the number:");
    scanf("%d", &num);
    sqrt(num);
    printf("The square of the given number is:", num);
}
int sqrt(int num)
{
    return num*num;
}
 
/* CONCATENATED FILE */
/*
Hello!! * this is temporary file for use in file handling
This is a C Program File. *
Consider a code to Add two numbers  */
*/
#include <stdio.h>
#include <stdio.h>
int sqrt(int);
/* Function Prototype */void main()
{
int sum(int,  int);    int num;
void main()    printf("enter the number:");
{    scanf("%d", &num);
    int num1, num2;    sqrt(num);
    printf("Enter Number1 and Number2:");    printf("The square of the given number is:", num);
    scanf("%d %d ", num1, num2);}
    sum(num1, num2);int sqrt(int num)
}{
    return num*num;
int sum(int a, int b)}
{
    return a + b;
}

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.