Skip to content

Commit 3f811ca

Browse files
committed
add: 14_file_handling 07
1 parent 8e59b2f commit 3f811ca

File tree

4 files changed

+222
-14
lines changed

4 files changed

+222
-14
lines changed

.vscode/settings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

05_constructors_and_overloading/11_book.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
// // Header files
44
#include <iostream>
5-
65
#include <string.h>
76

87
// // use namespace
@@ -64,13 +63,13 @@ class Book
6463
// // instance member function to set Book Title
6564
void setBookTitle(char *bookTitle)
6665
{
67-
strcpy(bookTitle, bookTitle);
66+
strcpy(this->bookTitle, bookTitle);
6867
}
6968

7069
// // instance member function to get Book Title
7170
char *getBookTitle(char *bookTitle)
7271
{
73-
strcpy(bookTitle, bookTitle);
72+
strcpy(bookTitle, this->bookTitle);
7473
return bookTitle;
7574
}
7675
};

14_file_handling/00_questions.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
06. Write a C++ program that counts the total number of characters, words and lines in the file.
1818

19-
07. There are 50 records in a file. Each record contains 6-character item-code, 20
20-
characters for item-name and an integer price. Write a program to read these
21-
records, arrange them in the descending order of price and write them in the same
22-
file, overwriting the earlier records.
19+
07. Define a cpp class Book having attributes bookId, bookTitle and bookPrice and appropriate getters and setters. Now, get n number of book records from user and store each record in a file names "books_data.dat" and then read data from file and display.
2320

2421
08. A file 'Employee.txt' contains empno and empname. Write a C++ program to add and
2522
read contents of this file and search for an employee whose name is 'XYZ'.
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// // Define a cpp class Book having attributes bookId, bookTitle and bookPrice and appropriate getters and setters. Now, get n number of book records from user and store each record in a file names "books_data.dat" and then read data from file and display
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <fstream>
6+
#include <cstring>
7+
8+
// // use namespace
9+
using namespace std;
10+
11+
#define MAX_BOOKS 50
12+
13+
// // define class Book
14+
class Book
15+
{
16+
public:
17+
// // static member variable
18+
static const unsigned int MAX_CHARS_IN_TITLE = 31;
19+
20+
private:
21+
// // instance member variables
22+
unsigned int bookId;
23+
char bookTitle[MAX_CHARS_IN_TITLE];
24+
double bookPrice;
25+
26+
public:
27+
// // constructors
28+
Book()
29+
{
30+
bookId = -1;
31+
bookPrice = -1;
32+
bookTitle[0] = 0;
33+
}
34+
35+
Book(int bookId, const char *bookTitle, double bookPrice)
36+
{
37+
this->bookId = bookId;
38+
strcpy(this->bookTitle, bookTitle);
39+
this->bookPrice = bookPrice;
40+
}
41+
42+
// // instance member function to set bookId
43+
void setBookId(int bookId)
44+
{
45+
if (bookId < 0) // if bookId is negative make it positive
46+
bookId = -bookId;
47+
48+
this->bookId = bookId;
49+
}
50+
51+
// // instance member function to get bookId
52+
unsigned int getBookId()
53+
{
54+
return bookId;
55+
}
56+
57+
// // instance member function to set bookTitle
58+
void setBookTitle(char *bookTitle)
59+
{
60+
strcpy(this->bookTitle, bookTitle);
61+
}
62+
63+
// // instance member function to get bookTitle
64+
const char *getBookTitle()
65+
{
66+
return bookTitle;
67+
}
68+
69+
// // instance member function to set bookPrice
70+
double setBookPrice(double bookPrice)
71+
{
72+
this->bookPrice = bookPrice;
73+
}
74+
75+
// // instance member function to get bookPrice
76+
double getBookPrice()
77+
{
78+
return bookPrice;
79+
}
80+
81+
// // instance member function to input and set book data
82+
void inputBookData()
83+
{
84+
int bookId;
85+
char bookTitle[Book::MAX_CHARS_IN_TITLE];
86+
double bookPrice;
87+
88+
// // Get book id
89+
cout << "\nEnter Book Id => ";
90+
cin >> bookId;
91+
92+
// // Get book title
93+
cout << "\nEnter Book Title (MAX_CHARS " << Book::MAX_CHARS_IN_TITLE - 1 << ") => ";
94+
cin.ignore();
95+
cin.getline(bookTitle, Book::MAX_CHARS_IN_TITLE);
96+
97+
// // Get book price
98+
cout << "\nEnter Book Price => ";
99+
cin >> bookPrice;
100+
101+
// // set data
102+
setBookId(bookId);
103+
setBookTitle(bookTitle);
104+
setBookPrice(bookPrice);
105+
}
106+
107+
// // instance member function to show book data
108+
void showBookData()
109+
{
110+
cout << "\nBook Id => " << bookId;
111+
cout << "\nBook Title => " << bookTitle;
112+
cout << "\nBook Price => " << bookPrice;
113+
}
114+
115+
// // instance member function to store book record
116+
int storeBookData()
117+
{
118+
if (bookId == -1 || bookPrice == -1)
119+
return 0; // book data not stored
120+
121+
// // specify file name
122+
const char *fileName = "books_data.dat";
123+
124+
// create an instance of ofstream for writing in a file
125+
ofstream fout;
126+
127+
// // open file in binary mode for writing and append data
128+
fout.open(fileName, ios::app | ios::binary);
129+
130+
// // check if the file is successfully opened
131+
if (!fout.is_open())
132+
return 0; // book data not stored
133+
134+
fout.write((char *)this, sizeof(*this));
135+
136+
return 1; // book data successfully stored
137+
}
138+
};
139+
140+
// // function to fetch books data from a fike and show
141+
void fetchAndShowBookData()
142+
{
143+
// // specify file name
144+
const char *fileName = "books_data.dat";
145+
146+
// create an instance of ifstream for reading from a file
147+
ifstream fin;
148+
149+
// // open file in binary mode for reading
150+
fin.open(fileName, ios::in | ios::binary);
151+
152+
// // check if the file is successfully opened
153+
if (!fin.is_open())
154+
{
155+
cout << "\nError: Unable to Open File...";
156+
return;
157+
}
158+
159+
// // create an instance of Book to store fetched data
160+
Book tempBook;
161+
162+
fin.read((char *)&tempBook, sizeof(tempBook));
163+
164+
while (!fin.eof())
165+
{
166+
cout << endl;
167+
tempBook.showBookData();
168+
cout << endl;
169+
fin.read((char *)&tempBook, sizeof(tempBook));
170+
}
171+
}
172+
173+
// // Main Function Start
174+
int main()
175+
{
176+
int n;
177+
178+
cout << "\nHow Many Books' Data You Want to Store (MAX " << MAX_BOOKS << ") => ";
179+
cin >> n;
180+
181+
// // invalid input
182+
if (n < 1 || n > MAX_BOOKS)
183+
{
184+
cout << "\n!!! Invalid Input..." << endl;
185+
return 0;
186+
}
187+
188+
// // dynamically allocate memory for n objects of Book
189+
Book *books = new Book[n];
190+
191+
// // input, set and store books data
192+
cout << "\n>>>>>>>>>> Enter Data of " << n << " Books <<<<<<<<<<<<<" << endl;
193+
for (int i = 0; i < n; i++)
194+
{
195+
cout << "\n>>>>>>>>>>> Enter Data of Book " << i + 1 << " <<<<<<<<<<<<" << endl;
196+
197+
// // input and set book data
198+
books[i].inputBookData();
199+
200+
// // store book data
201+
if (!(books[i].storeBookData()))
202+
{
203+
cout << "\n!!! Book Data Not Stored..." << endl;
204+
return 0;
205+
}
206+
}
207+
208+
// // books data stored successfully
209+
cout << "\nBooks Data Successfully Stored..." << endl;
210+
211+
// // read and show books data
212+
cout << "\n>>>>>>>>>> Books Data Stored In File <<<<<<<<<<<<<<";
213+
fetchAndShowBookData();
214+
215+
cout << endl; // Add new line
216+
cin.ignore();
217+
return 0;
218+
}
219+
// // Main Function End

0 commit comments

Comments
 (0)