-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec8_filehandling.cpp
86 lines (76 loc) Β· 1.63 KB
/
lec8_filehandling.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
/* Program to write to a file */
// int main()
// {
// //open the file
// ofstream fout; //fout is an object of ofstream class
// fout.open("hello.txt");
// fout<<"hello world"; // write to the file ; create the file if not present
// fout.close(); // release the resources
// return 0;
// }
/* Program to read from a file */
// int main()
// {
// //open the file
// ifstream fin;
// fin.open("hello.txt");
// char ch;
// while(!fin.eof())
// {
// //fin>>ch; // no space
// ch = fin.get(); // read with space
// cout<<ch;
// }
// fin.close();
// return 0;
// }
/* Real Life Example */
// int main()
// {
// vector<int>arr(5);
// cout<<"Enter the input : ";
// for(int i=0;i<5;i++)
// {
// cin>>arr[i];
// }
// ofstream fout;
// fout.open("input.txt");
// fout<<"Original array : \n";
// for(int i=0;i<5;i++)
// {
// fout<<arr[i]<<"\n";
// }
// fout<<"Sorted array : \n";
// sort(arr.begin() , arr.end());
// for(int i=0;i<5;i++)
// {
// fout<<arr[i]<<"\n";
// }
// fout.close();
// return 0;
// }
/* Program to read multiple lines from a file */
int main()
{
ofstream fout;
fout.open("hello.txt");
fout<<"hello world\n";
fout<<"hello world 2\n";
fout<<"hello world 3\n";
fout.close();
ifstream fin;
fin.open("hello.txt");
string s;
while(getline(fin,s))
{
//getline(fin,s);
cout<<s<<endl;
}
fin.close();
return 0;
}