-
Notifications
You must be signed in to change notification settings - Fork 0
/
52_file_opening2.cpp
64 lines (58 loc) · 1.82 KB
/
52_file_opening2.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
// file opening modes
// 1 - ios :: out -> output/write
// 2 - ios :: in -> input/read
// 3 - ios :: app -> append
// 4 - ios :: ate -> update
// 5 - ios :: binary -> binary
/*
#include<iostream>
#include<fstream>
using namespace std;
int main ()
{
ofstream fout;
fout.open("my_file5.txt",ios :: out );// ios :: out is exceptional because ofstream class has permission to write a file by default
fout<<"i am writting this in file 5";
fout.close();
ifstream fin;
fin.open("my_file5.txt", ios::in);// ios :: in is exceptional because ifstream class has permission to read a file by default
string str;
getline(fin,str);
cout<<"this was written in a file :"<<endl;
cout<<str;
fin.close();
return 0 ;
}
*/
// ios :: app is used to write in a file without erasing previous content of the file
/*
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("my_file5.txt", ios ::app); // ios :: app , this is used to append i.e; if we want to write in a file then previous content of file will not vanish and whatever we write it adds up in the file
fout << "i am writting this in file 5";
fout.close();
ifstream fin;
fin.open("my_file5.txt");
string str;
getline(fin, str);
cout << "this was written in a file :" << endl;
cout << str;
fin.close();
return 0;
}
*/
// ios :: trunc -> this deletes the previous content of file and replace it with a new one or To truncate an existing file to zero
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out;
out.open("my_file7.txt", ios ::trunc); // by default it clears old content and replace it with a new one also
out << "hi i am writting in a file" << endl;
return 0;
}