You are on page 1of 25

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : M.Subramanyam
Designation : Senior Lecturer
Branch : Computer Engg.
Institute : Q.Q.Govt.Polytechnic,
Hyderabad.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code :CM-304
Topic : Files
Duration : 50 Min
Sub Topic : Case Study
Teaching Aids : PPTs, Animations
CM304.90 1
Recap

 So far different file handling functions have


been covered like fgetc(), fgets(), getw(),
fscanf(), fread(), fwrite() etc.,
 Data can be accessed sequentially or randomly.
 Any type of data can be written to the file.
 Any type of data can be read from the file.

CM304.90 2
Objectives

On completion of this period, you would be able


to know
 Understand the importance of files.
 Write large amount of data to a file.
 Read large amount of data from a file.
 Manipulate the file (database).

CM304.90 3
Mini Project with Student Database

 Write a program for array of students to perform


the following operations
– 1) Add student record
– 2) View details of all students
– 3) Update any student name
– 4) Delete any student record
– 5) Search for a particular student record
 Student structure contains pin,name,marks in 5
subjects,total and average
CM304.90 4
Student Record Structure
 Student structure contains the following fields
struct student /*Definition of student structure*/
{
char pin[13];
char name[20];
int marks[5];
float total, average;
}s;

CM304.90 5
Functions in the application

 void record_add();
 void record_view();
 void record_update();
 void record_delete();
 void search();
 void disp_college_details();
 void draw_line();

CM304.90 6
Functions in the application
Contd..

 void record_add():
– Reads record of each student from the
keyboard.
– Record is temporarily stored in the RAM.
– Record which is in the RAM is appended to
the file.

CM304.90 7
Functions in the application
Contd..

 void record_view():
– Reads record of each student from the file.

– Record is stored in the RAM.

– Record is printed from the RAM to Monitor.

– This process is repeated until all the records


are displayed.
CM304.90 8
Functions in the application
Contd..

 void record_update():
 Student pin is primary key.
 Accepts student pin number from keyboard.
 Reads one record from the file.
 Compares user entered pin with the pin in the
file.

CM304.90 9
Functions in the application
Contd..

 The process continues until record is found or


end of file is encountered.
 If record is found it updates student name.
 Updates the modified record in file.
 If record is not found displays a message.

CM304.90 10
Functions in the application
Contd..

void record_delete():
 Student pin is primary key.
 Accepts student pin number from keyboard.
 Reads one record from the file.
 Compares user entered pin with the pin in the file.

CM304.90 11
Functions in the application
Contd..

 The process continues till end of file.


 Updates new temporary file with all the records
of the original file except the record which is to
be deleted.
 Removes the original file.
 Renames temporary file as original file.

CM304.90 12
Functions in the application
Contd..

 void disp_college_details():
 Prints name of the college and place.
 void draw_line():
 Draws a line.

CM304.90 13
Coding

CM304.90 14
CM304.90 15
CM304.90 16
CM304.90 17
CM304.90 18
CM304.90 19
CM304.90 20
CM304.90 21
CM304.90 22
Input

CM304.90 23
Output

CM304.90 24
Assignment

 Write a program for array of employees to


perform the following
 1) Add employee record
 2) display details of all employees
 3) Update any employee name
 4) Delete any employee record
 5) Search for a particular employee record
 6)Sort records according to empno
 .Employee structure contains empno, ename,
sal and designation
CM304.90 25