SlideShare a Scribd company logo
Using C++...
Hints
- Use binary file read/write to store employee structures effeciently
- You can find the size of struct Employee using sizeof(Employee).
- Structs can be written out to a file using write() function.
- write() function takes address to a memory and the number of bytes to be written to the file.
It may be easier to get first and last name using std::string as follows:
- You can easily convert a char[] to a string as follows
- It is possble to get a const char[] from an std::string using c_str() function. Step 1 Create a
struct that can store employee id, wages and first and last names. To keep things simple, we
assume that employee names are stored in charll, and we only store the first 16 characters of the
name. See below. struct Employee char fname [16] char Iname [16] int id double wages Step 2
write a program that adds an employee to the employees at file. add-employee Enter firstname
Captain Enter lastname: America. Enter id: 2 Enter wage: 5.00 This program will append this
record to the employees dat Step 3 write a program that prints the info of the employee with the
passed employee id. s prn-employee 2 America, Captain id wage $5.00
Solution
as C++ is known as powerful lanuage because of classess (superior in comparison with struct) so
class employee is created.
#include
class Employee
{
int id;
char fname[16];
char lname[16];
double wages;
public:
//To display record based on employee id
void diplay(int );
//To append the data of employee
void append();
};
void Empoyee::append()
{
fstream File;
File.open( “EMP.DAT”, ios::binary | ios::in | ios::out);
cin>>id;
cin.getline(fname, 20);
cin.getline(lname, 20);
cin>>wages;
File.write( (char*)this, sizeof(Employee));
File.close();
}
void Employee::Search(int data)
{
fstream File;
File.open( “EMP.DAT”, ios::binary | ios::in);
File.read((char*)this, sizeof(Employee));
cout << id<< ”==> ” << Employee<< endl;
File.close();
}
Still if you need structure , here is the code
#include
#include
using namespace std;
typedef struct employee
{
char fname[16];
char lname[16];
int id;
double wages
}emp;
int main()
{
//values are hard coded
emp apprentice[3];
strcpy(apprentice[0].fname, "john");
strcpy(apprentice[0].fname, "sina");
apprentice[0].id = 21;
apprentice[0].wages=2000;
strcpy(apprentice[1].fname, "under");
strcpy(apprentice[1].fname, "taker");
apprentice[1].id = 20;
apprentice[1].wages=3000;
strcpy(apprentice[2].fname, "under");
strcpy(apprentice[2].fname, "taker");
apprentice[2].id = 20;
apprentice[2].wages=3000;
// Serializing struct to employee.data
ofstream output_file("employee.data", ios::binary);
output_file.write((char*)&apprentice, sizeof(apprentice));
output_file.close();
// Reading from it
ifstream input_file("employee.data", ios::binary);
emp master[3];
input_file.read((char*)&master, sizeof(master));
for (emp e = 0; e < 3; e++)
{
cout << "Id #" << e << endl;
cout << "fname: " << master[e].fname << endl;
cout << "lname: " << master[e].lname << endl;
cout << "wages: " <

More Related Content

Similar to Using C++...Hints- Use binary file readwrite to store employee .pdf (20)

PDF
Type Of Inheritance in C++ language (Object-Oriented Programming )
mjahdalfqyh5
 
PPTX
OOPS 22-23 (1).pptx
SushmaGavaraskar
 
DOCX
Computer science project work
rahulchamp2345
 
RTF
project3
Russell Rawls
 
TXT
Advance C++notes
Rajiv Gupta
 
DOC
Brief Summary Of C++
Haris Lye
 
DOC
Cis247 i lab 2 of 7 employee class
sdjdskjd9097
 
PDF
in C++ Design a class named Employee The class should keep .pdf
adithyaups
 
DOCX
Pratik Bakane C++
pratikbakane
 
DOC
Cis247 a ilab 2 of 7 employee class
cis247
 
DOC
Cis247 a ilab 2 of 7 employee class
ccis224477
 
PPTX
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
PPTX
ONLINE STUDENT MANAGEMENT SYSTEM
Rohit malav
 
PDF
I keep on get a redefinition error and an undefined error.Customer.pdf
herminaherman
 
DOCX
Pratik Bakane C++
pratikbakane
 
DOCX
CIS 247C iLab 4 of 7: Composition and Class Interfaces
HomeWork-Fox
 
PDF
Files and data storage
Zaid Shabbir
 
DOCX
Cis247 i lab 2 of 7 employee class
sdjdskjd9097
 
DOCX
Be sure to read all of Chapters 8 and 9 before starting this assignm.docx
aman341480
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
Type Of Inheritance in C++ language (Object-Oriented Programming )
mjahdalfqyh5
 
OOPS 22-23 (1).pptx
SushmaGavaraskar
 
Computer science project work
rahulchamp2345
 
project3
Russell Rawls
 
Advance C++notes
Rajiv Gupta
 
Brief Summary Of C++
Haris Lye
 
Cis247 i lab 2 of 7 employee class
sdjdskjd9097
 
in C++ Design a class named Employee The class should keep .pdf
adithyaups
 
Pratik Bakane C++
pratikbakane
 
Cis247 a ilab 2 of 7 employee class
cis247
 
Cis247 a ilab 2 of 7 employee class
ccis224477
 
Data Structures asdasdasdasdasdasdasdasdasdasd
abdulrahman39ab
 
ONLINE STUDENT MANAGEMENT SYSTEM
Rohit malav
 
I keep on get a redefinition error and an undefined error.Customer.pdf
herminaherman
 
Pratik Bakane C++
pratikbakane
 
CIS 247C iLab 4 of 7: Composition and Class Interfaces
HomeWork-Fox
 
Files and data storage
Zaid Shabbir
 
Cis247 i lab 2 of 7 employee class
sdjdskjd9097
 
Be sure to read all of Chapters 8 and 9 before starting this assignm.docx
aman341480
 
pointer, structure ,union and intro to file handling
Rai University
 

More from fashionfolionr (20)

PDF
Discuss the benefits of in-built coding software within the Health I.pdf
fashionfolionr
 
PDF
Determine if each system is1. memoryless2. Invertible3. Causal.pdf
fashionfolionr
 
PDF
According to the Fick equation, what happens to J when X (the distanc.pdf
fashionfolionr
 
PDF
A 14 foot ladder is leaning against a building. If the bottom of the.pdf
fashionfolionr
 
PDF
Briefly discuss operating system, firmware, and utility programs..pdf
fashionfolionr
 
PDF
Assume that scores on the LSAT are approximately normally distribute.pdf
fashionfolionr
 
PDF
A polynomial may be represented as a linked list where each node cont.pdf
fashionfolionr
 
PDF
All answers must be in your own wordsWhat iswas The Great Law Wh.pdf
fashionfolionr
 
PDF
4. An upward shift in the Reserve Bank of Australias policy reactio.pdf
fashionfolionr
 
PDF
Write a function prototype for each of the following functions (Jus.pdf
fashionfolionr
 
PDF
Why is white collar crime is so difficult to address with law enforc.pdf
fashionfolionr
 
PDF
What is Eulers number and who is it named after Can you name some.pdf
fashionfolionr
 
PDF
Unless specifically legally permitted, agreements to suppress or eli.pdf
fashionfolionr
 
PDF
There are tremendous pressures on selected industrialized countries,.pdf
fashionfolionr
 
PDF
This week we covered Accounting and Payroll liabilities and the diff.pdf
fashionfolionr
 
PDF
t Assignment 2 Econ 201 Importance of scarcity. SolutionScarc.pdf
fashionfolionr
 
PDF
Stark Company has five employees. Employees paid by the hour receive.pdf
fashionfolionr
 
PDF
Silas, Cole, and Addison are on the same Learning Team at UOP. If ea.pdf
fashionfolionr
 
PDF
Q1 Discuss three types of accounting anomalies and define why they .pdf
fashionfolionr
 
PDF
QUESTION 2727. A profit center is the same as a revenue center.T.pdf
fashionfolionr
 
Discuss the benefits of in-built coding software within the Health I.pdf
fashionfolionr
 
Determine if each system is1. memoryless2. Invertible3. Causal.pdf
fashionfolionr
 
According to the Fick equation, what happens to J when X (the distanc.pdf
fashionfolionr
 
A 14 foot ladder is leaning against a building. If the bottom of the.pdf
fashionfolionr
 
Briefly discuss operating system, firmware, and utility programs..pdf
fashionfolionr
 
Assume that scores on the LSAT are approximately normally distribute.pdf
fashionfolionr
 
A polynomial may be represented as a linked list where each node cont.pdf
fashionfolionr
 
All answers must be in your own wordsWhat iswas The Great Law Wh.pdf
fashionfolionr
 
4. An upward shift in the Reserve Bank of Australias policy reactio.pdf
fashionfolionr
 
Write a function prototype for each of the following functions (Jus.pdf
fashionfolionr
 
Why is white collar crime is so difficult to address with law enforc.pdf
fashionfolionr
 
What is Eulers number and who is it named after Can you name some.pdf
fashionfolionr
 
Unless specifically legally permitted, agreements to suppress or eli.pdf
fashionfolionr
 
There are tremendous pressures on selected industrialized countries,.pdf
fashionfolionr
 
This week we covered Accounting and Payroll liabilities and the diff.pdf
fashionfolionr
 
t Assignment 2 Econ 201 Importance of scarcity. SolutionScarc.pdf
fashionfolionr
 
Stark Company has five employees. Employees paid by the hour receive.pdf
fashionfolionr
 
Silas, Cole, and Addison are on the same Learning Team at UOP. If ea.pdf
fashionfolionr
 
Q1 Discuss three types of accounting anomalies and define why they .pdf
fashionfolionr
 
QUESTION 2727. A profit center is the same as a revenue center.T.pdf
fashionfolionr
 
Ad

Recently uploaded (20)

PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PPTX
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
digestive system for Pharm d I year HAP
rekhapositivity
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
HEAD INJURY IN CHILDREN: NURSING MANAGEMENGT.pptx
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
How to Create Rental Orders in Odoo 18 Rental
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
Ad

Using C++...Hints- Use binary file readwrite to store employee .pdf

  • 1. Using C++... Hints - Use binary file read/write to store employee structures effeciently - You can find the size of struct Employee using sizeof(Employee). - Structs can be written out to a file using write() function. - write() function takes address to a memory and the number of bytes to be written to the file. It may be easier to get first and last name using std::string as follows: - You can easily convert a char[] to a string as follows - It is possble to get a const char[] from an std::string using c_str() function. Step 1 Create a struct that can store employee id, wages and first and last names. To keep things simple, we assume that employee names are stored in charll, and we only store the first 16 characters of the name. See below. struct Employee char fname [16] char Iname [16] int id double wages Step 2 write a program that adds an employee to the employees at file. add-employee Enter firstname Captain Enter lastname: America. Enter id: 2 Enter wage: 5.00 This program will append this record to the employees dat Step 3 write a program that prints the info of the employee with the passed employee id. s prn-employee 2 America, Captain id wage $5.00 Solution as C++ is known as powerful lanuage because of classess (superior in comparison with struct) so class employee is created. #include class Employee { int id; char fname[16]; char lname[16]; double wages; public: //To display record based on employee id void diplay(int ); //To append the data of employee void append(); }; void Empoyee::append()
  • 2. { fstream File; File.open( “EMP.DAT”, ios::binary | ios::in | ios::out); cin>>id; cin.getline(fname, 20); cin.getline(lname, 20); cin>>wages; File.write( (char*)this, sizeof(Employee)); File.close(); } void Employee::Search(int data) { fstream File; File.open( “EMP.DAT”, ios::binary | ios::in); File.read((char*)this, sizeof(Employee)); cout << id<< ”==> ” << Employee<< endl; File.close(); } Still if you need structure , here is the code #include #include using namespace std; typedef struct employee { char fname[16]; char lname[16]; int id; double wages }emp; int main() { //values are hard coded emp apprentice[3];
  • 3. strcpy(apprentice[0].fname, "john"); strcpy(apprentice[0].fname, "sina"); apprentice[0].id = 21; apprentice[0].wages=2000; strcpy(apprentice[1].fname, "under"); strcpy(apprentice[1].fname, "taker"); apprentice[1].id = 20; apprentice[1].wages=3000; strcpy(apprentice[2].fname, "under"); strcpy(apprentice[2].fname, "taker"); apprentice[2].id = 20; apprentice[2].wages=3000; // Serializing struct to employee.data ofstream output_file("employee.data", ios::binary); output_file.write((char*)&apprentice, sizeof(apprentice)); output_file.close(); // Reading from it ifstream input_file("employee.data", ios::binary); emp master[3]; input_file.read((char*)&master, sizeof(master)); for (emp e = 0; e < 3; e++) { cout << "Id #" << e << endl; cout << "fname: " << master[e].fname << endl; cout << "lname: " << master[e].lname << endl; cout << "wages: " <