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.
* C Program that Merges Lines Alternatively from 2 Files & Print Result
char file1, file2;
puts("enter the name of file 1"); /*getting the names of file to be concatenated*/
puts("enter the name of file 2");
FILE *fptr1, *fptr2, *fptr3;
fptr1=fopen(file1, "r"); /*opening the files in read only mode*/
fptr3=fopen("merge2.txt", "w+"); /*opening a new file in write,update mode*/
char ch1, ch2;
int n = 0, w = 0;
while (((ch1=fgetc(fptr1)) != EOF) && ((ch2 = fgetc(fptr2)) != EOF))
if (ch1 != EOF) /*getting lines in alternately from two files*/
fgets(str1, 199, fptr1);
if (str1 != 'n')
n++; /*counting no. of lines*/
if (ch2 != EOF)
fgets(str1, 199, fptr2);
if (str1 != 'n')
n++; /*counting no.of lines*/
while ((ch1 = fgetc(fptr3)) != EOF) /*countig no.of words*/
fscanf(fptr3, "%s", str1);
if (str1 != ' ' || str1 != 'n')
fprintf(fptr3, "\n\n number of lines = %d n number of words is = %d\n", n, w - 1);
/*appendig comments in the concatenated file*/
$ 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Buy Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs