Skip to content

Commit 0084fef

Browse files
committed
add: cpp-programming-questions 14_file_handling 09
1 parent fc8d21c commit 0084fef

File tree

2 files changed

+337
-7
lines changed

2 files changed

+337
-7
lines changed

14_file_handling/07_store_book/store_book.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Book
4949
}
5050

5151
// // instance member function to get bookId
52-
unsigned int getBookId()
52+
unsigned int getBookId() const
5353
{
5454
return bookId;
5555
}
@@ -61,7 +61,7 @@ class Book
6161
}
6262

6363
// // instance member function to get bookTitle
64-
const char *getBookTitle()
64+
const char *getBookTitle() const
6565
{
6666
return bookTitle;
6767
}
@@ -73,13 +73,13 @@ class Book
7373
}
7474

7575
// // instance member function to get bookPrice
76-
double getBookPrice()
76+
double getBookPrice() const
7777
{
7878
return bookPrice;
7979
}
8080

8181
// // instance member function to input and set book data
82-
void inputBookData()
82+
void inputBookData()
8383
{
8484
int bookId;
8585
char bookTitle[Book::MAX_CHARS_IN_TITLE];
@@ -105,15 +105,15 @@ class Book
105105
}
106106

107107
// // instance member function to show book data
108-
void showBookData()
108+
void showBookData() const
109109
{
110110
cout << "\nBook Id => " << bookId;
111111
cout << "\nBook Title => " << bookTitle;
112112
cout << "\nBook Price => " << bookPrice;
113113
}
114114

115115
// // instance member function to store book record
116-
int storeBookData()
116+
int storeBookData() const
117117
{
118118
if (bookId == -1 || bookPrice == -1)
119119
return 0; // book data not stored
@@ -138,7 +138,7 @@ class Book
138138
};
139139

140140
// // function to fetch books data from a fike and show
141-
void fetchAndShowBookData()
141+
void fetchAndShowBookData()
142142
{
143143
// // specify file name
144144
const char *fileName = "books_data.dat";
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
/*
2+
3+
A company has following details of their employees in the file 'emp.dat'
4+
a. Emp Id
5+
b. Emp Name
6+
c. Emp Address
7+
d. Emp Dept (Admin/Sales/Production/IT)
8+
e. Emp Phone
9+
f. Emp Age
10+
11+
Write a C++ program to read the above file. Create a new file such as adm.dat,
12+
sal.dat, pro.dat, IT.dat respectively to store the employee details according to their department.
13+
14+
*/
15+
16+
// // Header files
17+
#include <iostream>
18+
#include <fstream>
19+
#include <cstring>
20+
21+
// // use namespace
22+
using namespace std;
23+
24+
#define MAX_BOOKS 50
25+
26+
// // define class Employee
27+
class Employee
28+
{
29+
public:
30+
// // static member variables
31+
static const unsigned int MAX_CHARS_IN_ADDRESS = 51;
32+
static const unsigned int MAX_CHARS_IN_NAME = 31;
33+
static const unsigned int MAX_CHARS_IN_DEPARTMENT = 31;
34+
static const unsigned int MAX_CHARS_IN_PHONE = 11;
35+
36+
private:
37+
// // instance member variables
38+
unsigned int id, age;
39+
char name[MAX_CHARS_IN_NAME], address[MAX_CHARS_IN_ADDRESS], department[MAX_CHARS_IN_DEPARTMENT], phone[MAX_CHARS_IN_PHONE];
40+
41+
public:
42+
// // constructors
43+
Employee()
44+
{
45+
id = -1;
46+
age = -1;
47+
name[0] = 0;
48+
address[0] = 0;
49+
department[0] = 0;
50+
phone[0] = 0;
51+
}
52+
53+
Employee(int id, const char *name, int age, const char *phone, const char *address, const char *department)
54+
{
55+
this->id = id;
56+
this->age = age;
57+
strcpy(this->name, name);
58+
strcpy(this->phone, phone);
59+
strcpy(this->address, address);
60+
strcpy(this->department, department);
61+
}
62+
63+
// // instance member function to set id
64+
void setId(int id)
65+
{
66+
if (id < 0) // if id is negative make it positive
67+
id = -id;
68+
69+
this->id = id;
70+
}
71+
72+
// // instance member function to get id
73+
unsigned int getId()
74+
{
75+
return id;
76+
}
77+
78+
// // instance member function to set age
79+
void setAge(int age)
80+
{
81+
if (age < 0) // if age is negative make it positive
82+
age = -age;
83+
84+
this->age = age;
85+
}
86+
87+
// // instance member function to get age
88+
unsigned int getAge()
89+
{
90+
return age;
91+
}
92+
93+
// // instance member function to set name
94+
void setName(const char *name)
95+
{
96+
strcpy(this->name, name);
97+
}
98+
99+
// // instance member function to get name
100+
const char *getName()
101+
{
102+
return name;
103+
}
104+
105+
// // instance member function to set phone
106+
void setPhone(const char *phone)
107+
{
108+
strcpy(this->phone, phone);
109+
}
110+
111+
// // instance member function to get phone
112+
const char *getPhone()
113+
{
114+
return phone;
115+
}
116+
117+
// // instance member function to set address
118+
void setAddress(const char *address)
119+
{
120+
strcpy(this->address, address);
121+
}
122+
123+
// // instance member function to get address
124+
const char *getAddress()
125+
{
126+
return address;
127+
}
128+
129+
// // instance member function to set department
130+
void setDepartment(const char *department)
131+
{
132+
strcpy(this->department, department);
133+
}
134+
135+
// // instance member function to get department
136+
const char *getDepartment()
137+
{
138+
return department;
139+
}
140+
141+
// // instance member function to input and set employee data
142+
void inputEmployeeData()
143+
{
144+
int empId;
145+
char bookTitle[Employee::MAX_CHARS_IN_TITLE];
146+
double bookPrice;
147+
148+
// // Get book id
149+
cout << "\nEnter Employee Id => ";
150+
cin >> empId;
151+
152+
// // Get book title
153+
cout << "\nEnter Employee Title (MAX_CHARS " << Employee::MAX_CHARS_IN_TITLE - 1 << ") => ";
154+
cin.ignore();
155+
cin.getline(bookTitle, Employee::MAX_CHARS_IN_TITLE);
156+
157+
// // Get book price
158+
cout << "\nEnter Employee Price => ";
159+
cin >> bookPrice;
160+
161+
// // set data
162+
setEmployeeId(empId);
163+
setEmployeeTitle(bookTitle);
164+
setEmployeePrice(bookPrice);
165+
}
166+
167+
// // instance member function to show book data
168+
void showEmployeeData()
169+
{
170+
cout << "\nEmployee Id => " << empId;
171+
cout << "\nEmployee Title => " << bookTitle;
172+
cout << "\nEmployee Price => " << bookPrice;
173+
}
174+
175+
// // instance member function to store book record
176+
int storeEmployeeData()
177+
{
178+
if (empId == -1 || bookPrice == -1)
179+
return 0; // book data not stored
180+
181+
// // specify file name
182+
const char *fileName = "books_data.dat";
183+
184+
// create an instance of ofstream for writing in a file
185+
ofstream fout;
186+
187+
// // open file in binary mode for writing and append data
188+
fout.open(fileName, ios::app | ios::binary);
189+
190+
// // check if the file is successfully opened
191+
if (!fout.is_open())
192+
return 0; // book data not stored
193+
194+
fout.write((char *)this, sizeof(*this));
195+
196+
return 1; // book data successfully stored
197+
}
198+
};
199+
200+
// // function to fetch books data from a fike and show
201+
void fetchAndShowEmployeeData()
202+
{
203+
// // specify file name
204+
const char *fileName = "books_data.dat";
205+
206+
// create an instance of ifstream for reading from a file
207+
ifstream fin;
208+
209+
// // open file in binary mode for reading
210+
fin.open(fileName, ios::in | ios::binary);
211+
212+
// // check if the file is successfully opened
213+
if (!fin.is_open())
214+
{
215+
cout << "\nError: Unable to Open File...";
216+
return;
217+
}
218+
219+
// // create an instance of Employee to store fetched data
220+
Employee tempEmployee;
221+
222+
fin.read((char *)&tempEmployee, sizeof(tempEmployee));
223+
224+
while (!fin.eof())
225+
{
226+
cout << endl;
227+
tempEmployee.showEmployeeData();
228+
cout << endl;
229+
fin.read((char *)&tempEmployee, sizeof(tempEmployee));
230+
}
231+
}
232+
233+
// // function to search a book from file using book id
234+
void searchEmployeeById(int empId)
235+
{
236+
// // specify file name
237+
const char *fileName = "books_data.dat";
238+
239+
// create an instance of ifstream for reading from a file
240+
ifstream fin;
241+
242+
// // open file in binary mode for reading
243+
fin.open(fileName, ios::in | ios::binary);
244+
245+
// // check if the file is successfully opened
246+
if (!fin.is_open())
247+
{
248+
cout << "\nError: Unable to Open File...";
249+
return;
250+
}
251+
252+
// // create an instance of Employee to store fetched data
253+
Employee tempEmployee;
254+
int found = 0;
255+
256+
fin.read((char *)&tempEmployee, sizeof(tempEmployee));
257+
258+
while (!fin.eof())
259+
{
260+
if (tempEmployee.getEmployeeId() == empId)
261+
{
262+
cout << "\n>>>>>>>>> Employee Found <<<<<<<<" << endl;
263+
tempEmployee.showEmployeeData();
264+
found = 1;
265+
break;
266+
}
267+
268+
fin.read((char *)&tempEmployee, sizeof(tempEmployee));
269+
}
270+
271+
if (!found)
272+
cout << "\nThere is No Employee Data Stored having Employee Id " << empId << endl;
273+
}
274+
275+
// // Main Function Start
276+
int main()
277+
{
278+
int n;
279+
280+
cout << "\nHow Many Employees' Data You Want to Store (MAX " << MAX_BOOKS << ") => ";
281+
cin >> n;
282+
283+
// // invalid input
284+
if (n < 1 || n > MAX_BOOKS)
285+
{
286+
cout << "\n!!! Invalid Input..." << endl;
287+
return 0;
288+
}
289+
290+
// // dynamically allocate memory for n objects of Employee
291+
Employee *books = new Employee[n];
292+
293+
// // input, set and store books data
294+
cout << "\n>>>>>>>>>> Enter Data of " << n << " Employees <<<<<<<<<<<<<" << endl;
295+
for (int i = 0; i < n; i++)
296+
{
297+
cout << "\n>>>>>>>>>>> Enter Data of Employee " << i + 1 << " <<<<<<<<<<<<" << endl;
298+
299+
// // input and set book data
300+
books[i].inputEmployeeData();
301+
302+
// // store book data
303+
if (!(books[i].storeEmployeeData()))
304+
{
305+
cout << "\n!!! Employee Data Not Stored..." << endl;
306+
return 0;
307+
}
308+
}
309+
310+
// // books data stored successfully
311+
cout << "\nEmployees Data Successfully Stored..." << endl;
312+
313+
// // read and show books data
314+
cout << "\n>>>>>>>>>> Employees Data Stored In File <<<<<<<<<<<<<<";
315+
fetchAndShowEmployeeData();
316+
317+
// // get book id to search a book in file
318+
int empId;
319+
320+
cout << "\nEnter Employee Id to Search A Employee => ";
321+
cin >> empId;
322+
323+
// // search book
324+
searchEmployeeById(empId);
325+
326+
cout << endl; // Add new line
327+
cin.ignore();
328+
return 0;
329+
}
330+
// // Main Function End

0 commit comments

Comments
 (0)