-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession16.cpp
More file actions
82 lines (60 loc) · 1.32 KB
/
Session16.cpp
File metadata and controls
82 lines (60 loc) · 1.32 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
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
/*
File Handling
1. What is File
2. File IO Operations
Input -> To Read data from File into Parogram
Output -> To write data from Program into File
#include<fstream>
fstream
create file, write data and read data
ifstream
read data
ofstream
create file, write data
*/
// Write Data in File
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Student{
int roll;
string name;
int age;
public:
Student(int roll, string name, int age){
this->roll = roll;
this->name = name;
this->age = age;
}
void showStudentDetails(){
cout<<"Roll:"<<roll<<"\n";
cout<<"Name:"<<name<<"\n";
cout<<"Age:"<<age<<"\n";
}
void saveStudentDetails(){
ofstream out("student.csv");
out<<roll<<","<<name<<","<<age<<"\n";
out.close();
cout<<"Student Data Written in File\n";
}
};
int main()
{
// ofstream is a class used to write data in files
// out is an object and can be anyname of your choice
// MyStudents.txt is file name which is input to constructor
/*
ofstream out("MyStudents.txt");
// << operator
out<<"1, John, 20\n";
out<<"2, Jennie, 22\n";
out<<"3, Fionna, 25\n";
out.close(); // Releasing Memory
cout<<"Data Written in File\n";
*/
Student s1(101,"Fionna",22);
s1.showStudentDetails();
s1.saveStudentDetails();
return 0;
}