Skip to content

Commit a5557a2

Browse files
committed
feat : added I/O Streams
1 parent b001c0a commit a5557a2

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

18.IO Streams/1_write_In_file.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<fstream>
2+
#include<iostream>
3+
using namespace std;
4+
5+
int main(){
6+
ofstream ofs ("a.txt",ios::trunc);
7+
ofs<<"John"<<endl;
8+
ofs<<25<<endl;
9+
ofs<<"cs";
10+
ofs.close();
11+
}

18.IO Streams/2_read_a_file.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include<fstream>
2+
#include<iostream>
3+
using namespace std;
4+
5+
int main(){
6+
ifstream infile;
7+
infile.open("a.txt");
8+
if(!infile.is_open())
9+
cout<<"Unable to open file"<<endl;
10+
11+
string str,str2;
12+
int x;
13+
14+
infile>>str>>x>>str2;
15+
if(infile.eof()){
16+
cout<<"end of file reached"<<endl;
17+
infile.close();
18+
}
19+
cout<<"Name : "<<str<<endl<<"Roll No : "<<x<<endl<<"Branch : "<<str2<<endl;
20+
return 0;
21+
}

18.IO Streams/3_serialization.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include<fstream>
2+
#include<iostream>
3+
using namespace std;
4+
5+
class Student{
6+
public:
7+
string name, branch;
8+
int rollNo;
9+
10+
// constructor
11+
Student(string name= " ", int rollNo=0, string branch=" "){
12+
this->name = name;
13+
this->rollNo = rollNo;
14+
this->branch = branch;
15+
}
16+
17+
friend ofstream & operator<<(ofstream &ofs, Student &s);
18+
friend ifstream & operator>>(ifstream &ifs, Student &s);
19+
};
20+
21+
ofstream & operator<<(ofstream &ofs, Student &s){
22+
cout<<"Name : "<<s.name<<endl;
23+
cout<<"Roll No : "<<s.rollNo<<endl;
24+
cout<<"Branch : "<<s.branch<<endl;
25+
return ofs;
26+
}
27+
28+
ifstream & operator>>(ifstream &ifs, Student &s){
29+
ifs>>s.name>>s.rollNo>>s.branch;
30+
return ifs;
31+
}
32+
33+
int main(){
34+
Student s1("Niladri Ghosh",17,"CS");
35+
ifstream ifs("student.txt");
36+
ifs>>s1;
37+
ifs.close();
38+
39+
ofstream ofs("student.txt");
40+
if(!ofs.is_open())
41+
cout<<"Unable to open file"<<endl;
42+
ofs<<s1;
43+
ofs.close();
44+
45+
return 0;
46+
}

18.IO Streams/4_student_exercise.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<iostream>
2+
#include<fstream>
3+
using namespace std;
4+
5+
class Product{
6+
public:
7+
string name;
8+
float price;
9+
int quantity;
10+
11+
//constructor
12+
Product(string name = " ",float price = 0, int quantity = 0){
13+
this->name = name;
14+
this->price = price;
15+
this->quantity = quantity;
16+
}
17+
18+
friend ifstream & operator>>(ifstream &ifs, Product &p);
19+
friend ofstream & operator<<(ofstream &ofs, Product &p);
20+
};
21+
22+
ifstream & operator>>(ifstream &ifs, Product &p){
23+
ifs>>p.name>>p.price>>p.quantity;
24+
return ifs;
25+
}
26+
27+
ofstream & operator<<(ofstream &ofs, Product &p){
28+
cout<<"Name : "<<p.name<<endl;
29+
cout<<"Price : $"<<p.price<<endl;
30+
cout<<"Quantity : "<<p.quantity<<endl;
31+
return ofs;
32+
}
33+
34+
int main(){
35+
Product p1("Hennessey Cognac",399.99,60);
36+
ifstream ifs("product.txt");
37+
ifs>>p1;
38+
ifs.close();
39+
40+
ofstream ofs("product.txt");
41+
ofs<<p1;
42+
ofs.close();
43+
44+
return 0;
45+
}

18.IO Streams/a.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
John
2+
25
3+
cs

18.IO Streams/product.txt

Whitespace-only changes.

18.IO Streams/student.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy