Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
341 views
in JNTU BTech (CSE-I-Sem) C PROGRAMMING AND DATA STRUCTURES LAB by Goeduhub's Expert (7.1k points)

Write a C program to merge two files into a third file (i.e., the contents of the first file followed by those of the second are put in the third file)

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 570/- || For International Students- $12.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.1k points)
edited by
 
Best answer

Merge Two Files

To Know about file handling from beginning Click Hear.

Following is the program to copy contents of two files into a third file.

Example :

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20],fname2[20],fname3[20];
cout<<"Enter first file name with extension (like files.txt) : ";
gets(fname1);
fs.open(fname1,ios::in);
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1);
}
cout<<"Enter second file name with extension (like files.txt) : ";
gets(fname2);
cout<<"Enter target file name with extension (like filet.txt) : ";
gets(fname3);
ft.open(fname3,ios::app);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
fs.close();
fs.open(fname2,ios::in);
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch();
}

Output :

Before :

After :


For More JNTU C Programming Lab Experiments Click Here

3.3k questions

7.1k answers

393 comments

4.5k users

Related questions

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
...