-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10a - Employee
100 lines (67 loc) · 1.91 KB
/
10a - Employee
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*****************************
NOTE:
Before executing this code, make sure you change the working folder to the desktop otherwise the file won't be created.
To work with files, the working directory should be the same as that where the file is being created and the executable
./a.out will be run.
else you will have to specify the entire path of file in the filename which is a hectic task.
So make sure you do this.
Open terminal and use the following commands:
Mac/Linux users: cd Desktop
Windows users: I hate you ! :P
********************************/
#include <iostream>
#include <fstream>
using namespace std;
class Employee
{
public:
int empno;
char name[20];
float salary;
char depname[20];
void getdata()
{
cout<<"Employee number: "<<endl;
cin>>empno;
cout<<"Name: "<<endl;
cin>>name;
cout<<"Salary: "<<endl;
cin>>salary;
cout<<"Department Name: "<<endl;
cin>>depname;
}
};
void countStuff()
{
ifstream fin("Sample.txt"); //opening text file
int line=-1,word=-1,size; //will not count first word and last line so initial value is -1.
char ch;
int character;
fin.seekg(0,ios::end); //bring file pointer position to end of file
size=fin.tellg(); //count number of bytes till current postion for file pointer
fin.seekg(0,ios::beg); //bring position of file pointer to begining of file
while(fin)
{
fin.get(ch);
if(ch==' '||ch=='\n')
word++;
if(ch=='\n')
line++;
}
cout<<"Lines="<<line<<"\nWords="<<word<<endl;
cout<<"Characters: "<<size<<endl; //The size is same as number of characters.
fin.close(); //closing file
}
int main()
{
Employee e;
ofstream ofile("Sample.txt"); //creating a file
cout<<"Enter details: "<<endl;
e.getdata();
ofile<<"Name: "<<e.name<<endl;
ofile<<"Employee number: "<<e.empno<<endl;
ofile<<"Salary: "<<e.salary<<endl;
ofile<<"Department Name: "<<e.depname<<endl;
countStuff();
return 0;
}