SlideShare a Scribd company logo
Write a task that will perform some of the functions performed
by a simple employee database. The various functions will be
laid out below Data: Each record will consist of six attributes,
one if with is the primary key. The primary key will be
automatically assigned to a record when the record is created.
The fields for the record are as follows: ID: integer
(automatically generated by program) Last name: 20 character
max First Name: 20 character max Social Security Number: 11
character max (include ") Salary: float Age: integer The data
should be stored in a randomly accessible file. There should be
at least 20 records stored in the file Index: The index function
will be able to index the data on any given field in ascending or
descending order. The Index function should prompt the user to
enter the field to be indexed. In addition, they should indicate if
they want to index in ascending or descending order. To
complete this task the sorting should be done using merge sort,
heap sort or quick sort. Also, when you are indexing, you
should not move entire records The whole record should not be
read in the program. Only the primary key and the indexed field
should be stored in the program's memory Query: Extra Credit
The query function will allow the user to perform basic queries
by typing in SQL statements. This function should support the
following SQL statements: . Select from table (basically it
displays all records) . Select * from table where
Solution
/*
* C Program to Create Employee Record and Update it
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 200
struct emp
{
int id;
char *name;
}*emp1, *emp3;
void display();
void create();
void update();
FILE *fp, *fp1;
int count = 0;
void main(int argc, char **argv)
{
int i, n, ch;
printf("1] Create a Record ");
printf("2] Display Records ");
printf("3] Update Records ");
printf("4] Exit");
while (1)
{
printf(" Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
fp = fopen(argv[1], "a");
create();
break;
case 2:
fp1 = fopen(argv[1],"rb");
display();
break;
case 3:
fp1 = fopen(argv[1], "r+");
update();
break;
case 4:
exit(0);
}
}
}
/* To create an employee record */
void create()
{
int i;
char *p;
emp1 = (struct emp *)malloc(sizeof(struct emp));
emp1->name = (char *)malloc((size)*(sizeof(char)));
printf("Enter name of employee : ");
scanf(" %[^ ]s", emp1->name);
printf("Enter emp id : ");
scanf(" %d", &emp1->id);
fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
fwrite(emp1->name, size, 1, fp);
count++; // count to number of entries of records
fclose(fp);
}
/* Display the records in the file */
void display()
{
emp3=(struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
int i = 1;
if (fp1 == NULL)
printf(" File not opened for reading");
while (i <= count)
{
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name, size, 1, fp1);
printf(" %d %s",emp3->id,emp3->name);
i++;
}
fclose(fp1);
free(emp3->name);
free(emp3);
}
void update()
{
int id, flag = 0, i = 1;
char s[size];
if (fp1 == NULL)
{
printf("File cant be opened");
return;
}
printf("Enter employee id to update : ");
scanf("%d", &id);
emp3 = (struct emp *)malloc(1*sizeof(struct emp));
emp3->name=(char *)malloc(size*sizeof(char));
while(i<=count)
{
fread(&emp3->id, sizeof(emp3->id), 1, fp1);
fread(emp3->name,size,1,fp1);
if (id == emp3->id)
{
printf("Enter new name of emplyee to update : ");
scanf(" %[^ ]s", s);
fseek(fp1, -204L, SEEK_CUR);
fwrite(&emp3->id, sizeof(emp3->id), 1, fp1);
fwrite(s, size, 1, fp1);
flag = 1;
break;
}
i++;
}
if (flag != 1)
{
printf("No employee record found");
flag = 0;
}
fclose(fp1);
free(emp3->name); /* to free allocated memory */
free(emp3);
}

More Related Content

Similar to Write a task that will perform some of the functions performed by a s.docx (20)

PDF
Structures-2
arshpreetkaur07
 
DOCX
UNIT-II CP DOC.docx
JavvajiVenkat
 
PDF
The ProblemUsing C programming language write a program that simul.pdf
federaleyecare
 
PDF
C notes.pdf
Durga Padma
 
PPTX
Compiler design lab
ilias ahmed
 
PPTX
C Programming ppt for beginners . Introduction
raghukatagall2
 
PPT
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
DOCX
Design problem
Sanjay Kumar Chakravarti
 
PDF
I really need help with this Assignment Please in C programming not .pdf
pasqualealvarez467
 
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly
 
PDF
Make sure to make a copy of the Google Doc for this lab into.pdf
adityastores21
 
PDF
Practical_File_Of_Data_Structure.pdf
VishwasChoclaty1
 
DOCX
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
Blake0FxCampbelld
 
PDF
streams and files
Mariam Butt
 
PPTX
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
TriggeredZulkar
 
DOC
Cis 170 c ilab 7 of 7 sequential files
CIS321
 
DOCX
The program reads data from two files, itemsList-0x.txt and .docx
oscars29
 
PPT
L8 file
mondalakash2012
 
PDF
I have come code already but I cant quite get the output rig.pdf
kashishkochhar5
 
Structures-2
arshpreetkaur07
 
UNIT-II CP DOC.docx
JavvajiVenkat
 
The ProblemUsing C programming language write a program that simul.pdf
federaleyecare
 
C notes.pdf
Durga Padma
 
Compiler design lab
ilias ahmed
 
C Programming ppt for beginners . Introduction
raghukatagall2
 
Fundamental of C Programming Language and Basic Input/Output Function
imtiazalijoono
 
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
Design problem
Sanjay Kumar Chakravarti
 
I really need help with this Assignment Please in C programming not .pdf
pasqualealvarez467
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly
 
Make sure to make a copy of the Google Doc for this lab into.pdf
adityastores21
 
Practical_File_Of_Data_Structure.pdf
VishwasChoclaty1
 
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
Blake0FxCampbelld
 
streams and files
Mariam Butt
 
luckfuckfunctioneekefkfejewnfiwnfnenf.pptx
TriggeredZulkar
 
Cis 170 c ilab 7 of 7 sequential files
CIS321
 
The program reads data from two files, itemsList-0x.txt and .docx
oscars29
 
I have come code already but I cant quite get the output rig.pdf
kashishkochhar5
 

More from ajoy21 (20)

DOCX
Please complete the assignment listed below.Define and explain, us.docx
ajoy21
 
DOCX
Please cite sources for each question. Do not use the same sources f.docx
ajoy21
 
DOCX
Please choose one of the following questions to answer for this week.docx
ajoy21
 
DOCX
Please check the attachment for my paper.Please add citations to a.docx
ajoy21
 
DOCX
Please answer to this discussion post. No less than 150 words. Refer.docx
ajoy21
 
DOCX
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
ajoy21
 
DOCX
Please answer the question .There is no work count. PLEASE NUMBER .docx
ajoy21
 
DOCX
Please answer the following questions. Please cite your references..docx
ajoy21
 
DOCX
Please answer the following questions.1.      1.  Are you or.docx
ajoy21
 
DOCX
Please answer the following question with 200-300 words.Q. Discu.docx
ajoy21
 
DOCX
Please answer the following question Why do you think the US ha.docx
ajoy21
 
DOCX
Please answer the following questions. Define tunneling in the V.docx
ajoy21
 
DOCX
Please answer the following questions1. How can you stimulate the.docx
ajoy21
 
DOCX
Please answer the following questions very deeply and presicely .docx
ajoy21
 
DOCX
Please answer the following questions in an informal 1 ½ - 2-page es.docx
ajoy21
 
DOCX
Please answer the following questions in a response of 150 to 200 wo.docx
ajoy21
 
DOCX
Please answer these questions regarding the (TILA) Truth in Lending .docx
ajoy21
 
DOCX
Please answer the following question pertaining to psychology. Inc.docx
ajoy21
 
DOCX
Please answer the following questions in a response of 250 to 300 .docx
ajoy21
 
DOCX
Please answer the three questions completly. I have attached the que.docx
ajoy21
 
Please complete the assignment listed below.Define and explain, us.docx
ajoy21
 
Please cite sources for each question. Do not use the same sources f.docx
ajoy21
 
Please choose one of the following questions to answer for this week.docx
ajoy21
 
Please check the attachment for my paper.Please add citations to a.docx
ajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
ajoy21
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
ajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
ajoy21
 
Please answer the following questions. Please cite your references..docx
ajoy21
 
Please answer the following questions.1.      1.  Are you or.docx
ajoy21
 
Please answer the following question with 200-300 words.Q. Discu.docx
ajoy21
 
Please answer the following question Why do you think the US ha.docx
ajoy21
 
Please answer the following questions. Define tunneling in the V.docx
ajoy21
 
Please answer the following questions1. How can you stimulate the.docx
ajoy21
 
Please answer the following questions very deeply and presicely .docx
ajoy21
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
ajoy21
 
Please answer the following questions in a response of 150 to 200 wo.docx
ajoy21
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
ajoy21
 
Please answer the following question pertaining to psychology. Inc.docx
ajoy21
 
Please answer the following questions in a response of 250 to 300 .docx
ajoy21
 
Please answer the three questions completly. I have attached the que.docx
ajoy21
 

Recently uploaded (20)

PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
community health nursing question paper 2.pdf
Prince kumar
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 

Write a task that will perform some of the functions performed by a s.docx

  • 1. Write a task that will perform some of the functions performed by a simple employee database. The various functions will be laid out below Data: Each record will consist of six attributes, one if with is the primary key. The primary key will be automatically assigned to a record when the record is created. The fields for the record are as follows: ID: integer (automatically generated by program) Last name: 20 character max First Name: 20 character max Social Security Number: 11 character max (include ") Salary: float Age: integer The data should be stored in a randomly accessible file. There should be at least 20 records stored in the file Index: The index function will be able to index the data on any given field in ascending or descending order. The Index function should prompt the user to enter the field to be indexed. In addition, they should indicate if they want to index in ascending or descending order. To complete this task the sorting should be done using merge sort, heap sort or quick sort. Also, when you are indexing, you should not move entire records The whole record should not be read in the program. Only the primary key and the indexed field should be stored in the program's memory Query: Extra Credit The query function will allow the user to perform basic queries by typing in SQL statements. This function should support the following SQL statements: . Select from table (basically it displays all records) . Select * from table where Solution /* * C Program to Create Employee Record and Update it */
  • 2. #include <stdio.h> #include <stdlib.h> #include <string.h> #define size 200 struct emp { int id; char *name; }*emp1, *emp3; void display(); void create(); void update(); FILE *fp, *fp1; int count = 0; void main(int argc, char **argv) { int i, n, ch; printf("1] Create a Record "); printf("2] Display Records "); printf("3] Update Records ");
  • 3. printf("4] Exit"); while (1) { printf(" Enter your choice : "); scanf("%d", &ch); switch (ch) { case 1: fp = fopen(argv[1], "a"); create(); break; case 2: fp1 = fopen(argv[1],"rb"); display(); break; case 3: fp1 = fopen(argv[1], "r+"); update(); break; case 4: exit(0); } } }
  • 4. /* To create an employee record */ void create() { int i; char *p; emp1 = (struct emp *)malloc(sizeof(struct emp)); emp1->name = (char *)malloc((size)*(sizeof(char))); printf("Enter name of employee : "); scanf(" %[^ ]s", emp1->name); printf("Enter emp id : "); scanf(" %d", &emp1->id); fwrite(&emp1->id, sizeof(emp1->id), 1, fp); fwrite(emp1->name, size, 1, fp); count++; // count to number of entries of records fclose(fp); } /* Display the records in the file */ void display() { emp3=(struct emp *)malloc(1*sizeof(struct emp)); emp3->name=(char *)malloc(size*sizeof(char)); int i = 1;
  • 5. if (fp1 == NULL) printf(" File not opened for reading"); while (i <= count) { fread(&emp3->id, sizeof(emp3->id), 1, fp1); fread(emp3->name, size, 1, fp1); printf(" %d %s",emp3->id,emp3->name); i++; } fclose(fp1); free(emp3->name); free(emp3); } void update() { int id, flag = 0, i = 1; char s[size]; if (fp1 == NULL) { printf("File cant be opened"); return; } printf("Enter employee id to update : ");
  • 6. scanf("%d", &id); emp3 = (struct emp *)malloc(1*sizeof(struct emp)); emp3->name=(char *)malloc(size*sizeof(char)); while(i<=count) { fread(&emp3->id, sizeof(emp3->id), 1, fp1); fread(emp3->name,size,1,fp1); if (id == emp3->id) { printf("Enter new name of emplyee to update : "); scanf(" %[^ ]s", s); fseek(fp1, -204L, SEEK_CUR); fwrite(&emp3->id, sizeof(emp3->id), 1, fp1); fwrite(s, size, 1, fp1); flag = 1; break; } i++; } if (flag != 1) { printf("No employee record found"); flag = 0; } fclose(fp1);
  • 7. free(emp3->name); /* to free allocated memory */ free(emp3); }