-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadfile.cpp
More file actions
39 lines (29 loc) · 889 Bytes
/
readfile.cpp
File metadata and controls
39 lines (29 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
std::string filename;
std::string content, line;
cout << "Enter the name of the file to read: ";
getline(std::cin,filename);
// Open the file for reading
ifstream inputFile(filename.c_str());
if (inputFile.is_open()) {
// Read the contents of the file
while (getline(inputFile, line)) {
content += line + "\n";
}
// Display the contents of the file
cout << "Contents of " << filename << ":" << endl;
cout << content << endl;
} else {
// File not found
cout << "File not found" << endl;
}
// Close the file
inputFile.close();
cin.get();
return 0;
}