-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.cpp
More file actions
78 lines (69 loc) · 1.27 KB
/
fileio.cpp
File metadata and controls
78 lines (69 loc) · 1.27 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
//File input/output operations
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
class student
{public:
char name[20];
int rollno;
public:
student();
student(char[],int);
~student();
};
student::student()
{
}
student::student(char name[10],int rollno)
{
cout<<"this is a constructor";
strcpy(this->name,name);
this->rollno=rollno;
}
student::~student()
{
cout<<"destructor";
}
int main()
{
int j;
student s1[5];
student s2("",1);
ofstream i;
i.open("cpptextfile.txt");
for(j=0;j<5;j++)
{
cout<<"enter rollno"<<endl;
cin>>s1[j].rollno;
fflush(stdin);
cout<<"enter name"<<endl;
cin.getline(s1[j].name,20);
i.write((char*)&s1[j],sizeof(student));
}
char ch[20];
char str[20]="this is test";
i.close();
ifstream o;
try
{
o.open("cpptextfile.txt");
if(!o.is_open())
{
int a;
throw(a);
}
while(!o.eof())
{
o.read((char*)&s2,sizeof(student));
cout<<s2.name;
cout<<s2.rollno;
cout<<endl;
}
o.close();
}
catch(int a)
{
cout<<"file not found";
}
}