-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileReading.cc
More file actions
45 lines (34 loc) · 864 Bytes
/
fileReading.cc
File metadata and controls
45 lines (34 loc) · 864 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
40
41
42
43
44
45
// read file
#include <string>
#include <fstream>
#include <iostream>
int main(int argc, char const *argv[])
{
std::ifstream file("filename.txt"); // replace with your file name
if (!file) {
std::cerr << "Unable to open file\n";
return 1; // return with error code 1
}
std::string line;
while (std::getline(file, line)) {
std::cout << line << '\n';
}
file.close();
return 0;
}
// Write file
#include <string>
#include <fstream>
#include <iostream>
int main(int argc, char const *argv[])
{
std::ofstream file("filename.txt"); // replace with your file name
if (!file) {
std::cerr << "Unable to open file\n";
return 1; // return with error code 1
}
std::string line = "This is a line to write to the file.";
file << line << '\n';
file.close();
return 0;
}