- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteFiles.cpp
More file actions
Latest commit
51 lines (39 loc) · 1.13 KB
/
Copy pathReadWriteFiles.cpp
File metadata and controls
51 lines (39 loc) · 1.13 KB
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
46
47
48
49
50
51
#include<iostream>
#include<fstream>
#include<string>
// But what if, we have data which may or may not be there in the file!!! How do we know???
// What happends if the file could not be read? what happens if we couldn't load the file if it's not present? etc...
intmain()
{
std::fstream myFile;
// Writing in the file
myFile.open("Hello.txt", std::ios::out); // write
if (myFile.is_open()) {
myFile << "Hello Joy!\n";
myFile << "Welcome to the C++ IO System!\n";
myFile.close();
}
//myFile.open("Hello.txt", std::ios::out); // It will overwrite the file content
//if (myFile.is_open()) {
// myFile << "Hello Joy!\n";
// myFile.close();
//}
myFile.open("Hello.txt", std::ios::app); // append
if (myFile.is_open()) {
myFile << "C++ is too hard :(\n";
myFile.close();
}
// Reading file contents
std::string myString;
myFile.open("Hello.txt", std::ios::in); // read
if (myFile.is_open()) {
std::string fileContent;
while (std::getline(myFile, fileContent)) {
std::cout << fileContent << std::endl;
myString.append(fileContent);
}
myFile.close();
}
std::cout << myString << std::endl;
std::cin.get();
}