Skip to content

Commit 60990ae

Browse files
committed
add: 14_file_handling 05
1 parent 111ee3a commit 60990ae

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"fstream": "cpp"
4+
}
5+
}

14_file_handling/00_questions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
04. Write a program to copy the content of one text file to another while changing the case of every alphabet.
1414

15-
05. Write a C++ program to merge the two files.
15+
05. Write a C++ program to merge the content of two files and generate a new file
1616

1717
06. Write a C++ program that counts the total number of characters, words and lines in the file.
1818

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// // Write a C++ program to merge the content of two files and generate a new file
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <fstream>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
int main()
11+
{
12+
// // specify files names
13+
const char *file1 = "file1.txt";
14+
const char *file2 = "file2.txt";
15+
const char *mergedFile = "merged_file.txt";
16+
17+
// // create an instances of fstream for both reading and writing in a file
18+
fstream fcout, fcin;
19+
20+
// // open file1 for reading
21+
fcin.open(file1, ios::in);
22+
23+
// // check if the file is successfully opened
24+
if (!fcin.is_open())
25+
{
26+
cout << "\nError: Unable to Open File1...\n";
27+
return 1;
28+
}
29+
30+
// // open file for writing
31+
fcout.open(mergedFile, ios::out);
32+
33+
// // check if the file is successfully opened
34+
if (!fcout.is_open())
35+
{
36+
cout << "\nError: Unable to Open or Create New Merged File...\n";
37+
return 1;
38+
}
39+
40+
// // read characters from file1 one by one and write in merged file
41+
char ch; // to store character
42+
43+
do
44+
{
45+
// // read one character from file
46+
ch = fcin.get();
47+
48+
// // if not reached to the end of file
49+
if (!fcin.eof())
50+
fcout << ch;
51+
52+
} while (!fcin.eof());
53+
54+
// // close file1
55+
fcin.close();
56+
57+
// // open file2 for reading
58+
fcin.open(file2, ios::in);
59+
60+
// // check if the file is successfully opened
61+
if (!fcin.is_open())
62+
{
63+
cout << "\nError: Unable to Open File1...\n";
64+
return 1;
65+
}
66+
67+
// // add space in merged file
68+
fcout << " ";
69+
70+
// // read characters from file2 one by one and write in merged file
71+
do
72+
{
73+
// // read one character from file
74+
ch = fcin.get();
75+
76+
// // if not reached to the end of file
77+
if (!fcin.eof())
78+
fcout << ch;
79+
80+
} while (!fcin.eof());
81+
82+
// // close files
83+
fcout.close();
84+
fcin.close();
85+
86+
// // files merged successfully
87+
cout << "\nContent of the Files Merged Successfully..." << endl;
88+
89+
cout << endl; // Add new line
90+
return 0;
91+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Programming is My
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOVE
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Code is My LOVE

0 commit comments

Comments
 (0)