Skip to content

Commit 2726181

Browse files
committed
add: cpp-programming-questions
1 parent 60990ae commit 2726181

File tree

13 files changed

+205
-52
lines changed

13 files changed

+205
-52
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"files.associations": {
3-
"fstream": "cpp"
3+
"fstream": "cpp",
4+
"*.tcc": "cpp"
45
}
56
}

14_file_handling/02_read_file_and_cout_chars/02_read_file_and_cout_chars.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ int main()
1414
const char *fileName = "read.txt";
1515

1616
// // create an instance of ifstream for reading from a file
17-
ifstream fcin;
17+
ifstream fin;
1818

1919
// // open file for reading
20-
fcin.open(fileName, ios::in);
20+
fin.open(fileName, ios::in);
2121

2222
// // check if the file is successfully opened
23-
if (!fcin.is_open())
23+
if (!fin.is_open())
2424
{
2525
cout << "\nError: Unable to Open File..." << endl;
2626
return 1;
@@ -30,28 +30,14 @@ int main()
3030
char ch; // to store character
3131
int counter = 0; // to count characters
3232

33-
// // get one character from file
34-
ch = fcin.get();
35-
36-
if (fcin.eof())
37-
cout << "\nFile is Empty...";
38-
else
39-
cout << "\n>>>>>>>>> Following is the Content Read From File <<<<<<<<<<\n";
40-
41-
while (!fcin.eof()) // run till the end of the file
42-
{
43-
cout << ch; // display read character
44-
counter++; // increment counter
45-
46-
// // get one character from file
47-
ch = fcin.get();
48-
}
33+
while (fin.get(ch)) // run till the end of the file
34+
counter++; // increment counter
4935

5036
// // display number of characters
5137
cout << "\n\nNumber of Characters in File => " << counter << endl;
5238

5339
// // close file
54-
fcin.close();
40+
fin.close();
5541

5642
cout << endl; // Add new line
5743
return 0;

14_file_handling/04_copy_file/04_copy_file.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ int main()
1414
const char *destinationFile = "destination.txt";
1515

1616
// // create an instances of fstream for both reading and writing in a file
17-
fstream fcout, fcin;
17+
fstream fout, fin;
1818

1919
// // open file for reading
20-
fcin.open(sourceFile, ios::in);
20+
fin.open(sourceFile, ios::in);
2121

2222
// // check if the file is successfully opened
23-
if (!fcin.is_open())
23+
if (!fin.is_open())
2424
{
2525
cout << "\nError: Unable to Open Source File...\n";
2626
return 1;
2727
}
2828

2929
// // open file for writing
30-
fcout.open(destinationFile, ios::out);
30+
fout.open(destinationFile, ios::out);
3131

3232
// // check if the file is successfully opened
33-
if (!fcout.is_open())
33+
if (!fout.is_open())
3434
{
3535
cout << "\nError: Unable to Open or Create Destination File...\n";
3636
return 1;
@@ -42,7 +42,7 @@ int main()
4242
do
4343
{
4444
// // read one character from file
45-
ch = fcin.get();
45+
ch = fin.get();
4646

4747
// // if character in uppercase, then convert it in lowercase and vice-versa
4848
if (ch >= 'A' && ch <= 'Z')
@@ -51,14 +51,14 @@ int main()
5151
ch -= 32;
5252

5353
// // if not reached to the end of file
54-
if (!fcin.eof())
55-
fcout << ch;
54+
if (!fin.eof())
55+
fout << ch;
5656

57-
} while (!fcin.eof());
57+
} while (!fin.eof());
5858

5959
// // close files
60-
fcout.close();
61-
fcin.close();
60+
fout.close();
61+
fin.close();
6262

6363
// // file copied successfully
6464
cout << "\nFile Content Successfully Copied..." << endl;

14_file_handling/05_merge_two_files/05_merge_two_files.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@ int main()
1515
const char *mergedFile = "merged_file.txt";
1616

1717
// // create an instances of fstream for both reading and writing in a file
18-
fstream fcout, fcin;
18+
fstream fout, fin;
1919

2020
// // open file1 for reading
21-
fcin.open(file1, ios::in);
21+
fin.open(file1, ios::in);
2222

2323
// // check if the file is successfully opened
24-
if (!fcin.is_open())
24+
if (!fin.is_open())
2525
{
2626
cout << "\nError: Unable to Open File1...\n";
2727
return 1;
2828
}
2929

3030
// // open file for writing
31-
fcout.open(mergedFile, ios::out);
31+
fout.open(mergedFile, ios::out);
3232

3333
// // check if the file is successfully opened
34-
if (!fcout.is_open())
34+
if (!fout.is_open())
3535
{
3636
cout << "\nError: Unable to Open or Create New Merged File...\n";
3737
return 1;
@@ -43,45 +43,45 @@ int main()
4343
do
4444
{
4545
// // read one character from file
46-
ch = fcin.get();
46+
ch = fin.get();
4747

4848
// // if not reached to the end of file
49-
if (!fcin.eof())
50-
fcout << ch;
49+
if (!fin.eof())
50+
fout << ch;
5151

52-
} while (!fcin.eof());
52+
} while (!fin.eof());
5353

5454
// // close file1
55-
fcin.close();
55+
fin.close();
5656

5757
// // open file2 for reading
58-
fcin.open(file2, ios::in);
58+
fin.open(file2, ios::in);
5959

6060
// // check if the file is successfully opened
61-
if (!fcin.is_open())
61+
if (!fin.is_open())
6262
{
6363
cout << "\nError: Unable to Open File1...\n";
6464
return 1;
6565
}
6666

6767
// // add space in merged file
68-
fcout << " ";
68+
fout << " ";
6969

7070
// // read characters from file2 one by one and write in merged file
7171
do
7272
{
7373
// // read one character from file
74-
ch = fcin.get();
74+
ch = fin.get();
7575

7676
// // if not reached to the end of file
77-
if (!fcin.eof())
78-
fcout << ch;
77+
if (!fin.eof())
78+
fout << ch;
7979

80-
} while (!fcin.eof());
80+
} while (!fin.eof());
8181

8282
// // close files
83-
fcout.close();
84-
fcin.close();
83+
fout.close();
84+
fin.close();
8585

8686
// // files merged successfully
8787
cout << "\nContent of the Files Merged Successfully..." << endl;

14_file_handling/05_merge_two_files/merged_file.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// // Write a C++ program that counts the total number of characters, words and lines in the file.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <fstream>
6+
7+
#define WORD_SIZE 50
8+
9+
// // use namespace
10+
using namespace std;
11+
12+
int main()
13+
{
14+
15+
// // specify file name
16+
const char *fileName = "read.txt";
17+
18+
// // create an instance of ifstream for reading from a file
19+
ifstream fin;
20+
21+
// // open file for reading
22+
fin.open(fileName, ios::in);
23+
24+
// // check if the file is successfully opened
25+
if (!fin.is_open())
26+
{
27+
cout << "\nError: Unable to Open File..." << endl;
28+
return 1;
29+
}
30+
31+
// // read file character by character and count the characters, words and lines
32+
char ch; // to store character
33+
int charCounter = 0, wordCounter = 0, lineCounter = 0;
34+
char word[WORD_SIZE]; // to store word extracted from file
35+
36+
// // get one character from file
37+
ch = fin.get();
38+
39+
if (fin.eof())
40+
cout << "\nFile is Empty...";
41+
else
42+
{
43+
cout << "\n>>>>>>>>> Following is the Content Read From File <<<<<<<<<<\n";
44+
lineCounter++;
45+
}
46+
47+
while (!fin.eof()) // run till the end of the file
48+
{
49+
cout << "\ntell g => " << fin.tellg();
50+
51+
cout << ch; // display read character
52+
charCounter++; // increment charCounter
53+
54+
// // if new line character occurs
55+
if (ch == '\n')
56+
lineCounter++;
57+
58+
// // get one character from file
59+
ch = fin.get();
60+
}
61+
62+
cout << "\ntell g => " << fin.tellg();
63+
64+
fin.seekg(1);
65+
66+
cout << "\ntell g => " << fin.tellg();
67+
68+
while (fin >> word) // run till the end of the file
69+
wordCounter++;
70+
71+
// // display number of characters,words and lines
72+
cout << "\n\nNumber of Characters in File => " << charCounter << endl;
73+
cout << "Number of Words in File => " << wordCounter << endl;
74+
cout << "Number of Lines in File => " << lineCounter << endl;
75+
76+
// // close file
77+
fin.close();
78+
79+
cout << endl; // Add new line
80+
return 0;
81+
}
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aman kaum
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// // Write a C++ program that counts the total number of characters, words and lines in the 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+
13+
// // specify file name
14+
const char *fileName = "read.txt";
15+
16+
// // create an instance of ifstream for reading from a file
17+
ifstream fin;
18+
19+
// // open file for reading
20+
fin.open(fileName, ios::in);
21+
22+
// // check if the file is successfully opened
23+
if (!fin.is_open())
24+
{
25+
cout << "\nError: Unable to Open File..." << endl;
26+
return 1;
27+
}
28+
29+
// // read file character by character and count the characters, words and lines
30+
char ch; // to store character
31+
int counter = 0;
32+
33+
while (!fin.eof()) // run till the end of the file
34+
{
35+
cout << ch; // display read character
36+
counter++; // increment charCounter
37+
38+
// // get one character from file
39+
ch = fin.get();
40+
}
41+
42+
// // display number of characters,words and lines
43+
cout << "\n\nNumber of Characters in File => " << counter << endl;
44+
45+
// // close file
46+
fin.close();
47+
48+
cout << endl; // Add new line
49+
return 0;
50+
}
46.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)