Skip to content

Commit feb29fb

Browse files
committed
add: 14_file_handling
1 parent 81357f2 commit feb29fb

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
🟢 Topic ➡ File Handling
55

66

7-
01. Write a C++ program to create a file and print “File created successfully” and throw an error if file is not created.
7+
01. Write a C++ program to create a file and print “File created successfully”,otherwise display "Unable to Create a File".
88

99
02. Write a C++ program to read a text file and count the number of characters in it.
1010

11-
03. Write a C++ program to open an output file 'a.txt' and append data to it.
11+
03. Write a C++ program to open a file 'output.txt' and append data to it.
1212

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

14_file_handling/01_create_file/01_create_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// // Write a C++ program to create a file and print “File created successfully” and throw an error if file is not created.
1+
// // Write a C++ program to create a file and print “File created successfully”,otherwise display "Unable to Create a File"
22

33
// // Header files
44
#include <iostream>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// // Write a C++ program to open a file 'output.txt' and append data to it.
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 file name
13+
const char *fileName = "output.txt";
14+
const char *appendData = "Cpp Programming";
15+
16+
// // create an instance of ofstream
17+
ofstream fout;
18+
19+
// // open file for writing, if file not exist it will be created
20+
fout.open(fileName, ios::app);
21+
22+
// // check if the file is successfully opened
23+
if (!fout.is_open())
24+
{
25+
cout << "\n!!! Unable to Open File...\n";
26+
return 1;
27+
}
28+
29+
// // write data in file using insertion operator
30+
fout << appendData;
31+
32+
cout << "\nData Appended Successfully..." << endl;
33+
34+
// // close file
35+
fout.close();
36+
37+
cout << endl; // Add new line
38+
return 0;
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
We Are Learning

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The repository is structured based on different topics, with each topic having i
1919
- /11_new_and_delete
2020
- /12_overloading_overriding_constructor_in_inheritance
2121
- /13_virtual_function_and_abstract_class
22+
- /14_file_handling
2223

2324
## Clone this Repository
2425

0 commit comments

Comments
 (0)