C Program to Merge Alternate Lines from Two Files

This C Program merges lines alternatively from 2 files & print result.

Here is source code of the C Program to merge lines alternatively from 2 files & print result. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program that Merges Lines Alternatively from 2 Files & Print Result
  3.  */
  4. #include<stdio.h>
  5. main()
  6. {
  7.     char file1[10], file2[10];
  8.  
  9.     puts("enter the name of file 1");      /*getting the names of file to be concatenated*/
  10.     scanf("%s", file1);
  11.     puts("enter the name of file 2");
  12.     scanf("%s", file2);
  13.     FILE *fptr1, *fptr2, *fptr3;
  14.     fptr1=fopen(file1, "r");             /*opening the files in read only mode*/
  15.     fptr2=fopen(file2, "r");
  16.     fptr3=fopen("merge2.txt", "w+");   /*opening a new file in write,update mode*/
  17.     char str1[200];
  18.     char ch1, ch2;
  19.     int n = 0, w = 0;
  20.     while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
  21.     {
  22.         if (ch1 != EOF)             /*getting lines in alternately from two files*/
  23.         {
  24.             ungetc(ch1, fptr1);
  25.             fgets(str1, 199, fptr1);
  26.             fputs(str1, fptr3);
  27.             if (str1[0] != 'n')
  28.                 n++;      /*counting no. of lines*/
  29.         }
  30.         if (ch2 != EOF)
  31.         {
  32.             ungetc(ch2, fptr2);
  33.             fgets(str1, 199, fptr2);
  34.             fputs(str1, fptr3);
  35.             if (str1[0] != 'n')
  36.                 n++;        /*counting no.of lines*/
  37.         }
  38.     }
  39.     rewind(fptr3);
  40.     while ((ch1 = fgetc(fptr3)) != EOF)       /*countig no.of words*/
  41.     {
  42.         ungetc(ch1, fptr3);
  43.         fscanf(fptr3, "%s", str1);
  44.         if (str1[0] != ' ' || str1[0] != 'n')
  45.             w++;
  46.     }
  47.     fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n", n, w - 1);
  48.     /*appendig comments in the concatenated file*/
  49.     fclose(fptr1);
  50.     fclose(fptr2);
  51.     fclose(fptr3);
  52. }

$ cc pgm51.c
$ a.out
enter the name of file 1
c.txt
enter the name of file 2
a.txt
$ vi merge2.txt
All participants will be provided with 1:1 Linux Systems for Lab work. If participants want, they can bring their own laptops with Linux in it. This enable them to do lot of quality assignments even Sanfoundry internship programs are great learning opportunities.
Students with proven credentials only are enrolled for this program and the duration of these programs ranges from 2-6 months full t after the classes are over. If they have Windows, we install virtualization software and Ubuntu Linux virtual appliance on top of windows system.
ime. Student must be passionate about Technology topics. As part of Sanfoundry's biggest open learning initiative, we are looking for interns (student or working professional) in following technolog
 
 number of lines = 4
 number of words is = 114

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 other example programs on File Handling, go to File Handling. 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.