-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52File_Reading_Writing.cpp
More file actions
40 lines (32 loc) · 1.13 KB
/
Copy path52File_Reading_Writing.cpp
File metadata and controls
40 lines (32 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
#include <iostream>
#include<fstream>
using namespace std;
/*
The useful classes for working with files in C++ are:
1. fstreambase
2. ifstream --> dervied from fstreambase
3. ofstream --> dervied from fstreambase
*/
/*
In order to work with files in C++, you will have to open it. Primarily, there are 2 ways to open
a file:
1. using the constructor
2. using the member function open() of the class
using namespace std;
*/
int main(){
string st = "Harshit: How you doin'?";
string st2;
//********Opening files using constructor and writing it******
ofstream out("52sample.txt"); // write operation
out<<st<<endl;
out<<st<<endl;
out<<st<<endl;
//********Opening files using constructor and reading it******
ifstream in("52sampleb.txt"); // reading operation
// in>>st2; // if we only used this then we will only get the first word of the line, not the entire sentence
getline(in, st2); // this allows you to get whole line, but only first line, not the line next to it
getline(in, st2); // but if we run this function again we will get product of next line, and so on
cout<<st2<<endl;
return 0;
}