-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReadingAndWritingFilesIn.cpp
More file actions
36 lines (30 loc) · 1.03 KB
/
FileReadingAndWritingFilesIn.cpp
File metadata and controls
36 lines (30 loc) · 1.03 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
/*
These are some useful classes for working with files in C++
fstreambase
ifstream --> derived from fstreambase
ofstream --> derived from fstreambase
*/
// In oredr to work with files in c++ , you will have to open it .
// primarily there are two ways to open a file :
// 1. Using the Constructor
// 2. Using the member function open() of the class
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string st = "Anshul Rai";
// Opening files using constructor and writing it
ofstream out("sample60.txt"); // Write operation
// ofstream --> Write operation
// out --> Object (you can change it by your choise)
// ("sample60.txt")---> the file we are acceseing.
out << st;
string st2;
// Opening files using constructor and reading it
ifstream in("sample60b.txt"); // Read operation
getline(in, st2);
in >> st2;
cout << st2;
return 0;
}