Case Study On C
Introduction
A case study in C language refers to a detailed real-world problem-solving example or project that demonstrates the application of C programming concepts to develop a solution.
Purpose of a Case Study:
To apply theoretical knowledge of C in practical scenarios.
To understand program structure, logic building, and modular design.
To improve skills in problem analysis, algorithm development, and code implementation.
Memory Allocation of Data
Static Memory Allocation
Static Memory Allocation in C refers to the process of allocating memory at compile time. The memory size and type are fixed when the program is compiled, and it cannot be changed at runtime. Exists throughout the program execution (for global/static variables). Once the program execution is completed then, the memory is erased.
For example, if you declared variables like…
int a, b[20];
Dynamic Memory Allocation
Dynamic Memory in C refers to the process of allocating memory at runtime using special functions. This allows programs to request memory as needed, making them more flexible and efficient compared to static memory allocation.
Memory is allocated in heap by using functions malloc(), calloc() and realloc().
Temporary Storage
Temporary storage refers to volatile memory that is used to store data temporarily, commonly, variable memory is volatile memory. its memory is allocated in the primary memory device (RAM).
Permanent Storage
Permanent storage refers to non-volatile memory where data is stored permanently or until manually deleted. Like
Hard Disk Drive (HDD)
Solid State Drive (SSD)
USB Flash Drive
CD/DVD
ROM (Read Only Memory)
Data File / File Oraganization
A data file in C is an external storage file (like .txt, .dat, etc.) used to store data permanently. This allows a C program to read from or write to a file, even after the program ends.
Why Use Data Files?
To store data permanently (unlike RAM).
To share data between program runs.
To work with large amounts of data efficiently.
How to work with data files.
To work with data files, C provides functions in stdio.h
There are 4 steps to work with data files.
- Create a pointer variable to a pre-defined structure FILE.
- Open a file by using fopen() function.
- Perform read, write operations on data files.
- Close the file.
What is FILE?
In C programming, FILE is a predefined data type (structure) defined in the stdio.h header file. It is used to represent and store information about a file being used in a program.
File is a structure in C, it contains information about a file. Like
- File Descriptor
- Current Position in the file.
- Error , EOF indicators.
- Buffering details and so on…
You don’t usually manipulate the FILE structure directly. Instead, you use pointers to FILE, like FILE *fp, and interact with files using standard library functions
Syntax:
typedef struct FILE {
int fd; /* File descriptor */
char *buffer; /* Data buffer */
int buffer_size; /* Size of buffer */
int position; /* Current position */
int eof; /* End-of-file flag */
int error; /* Error flag */
}FILE;
Declaration:
FILE *fp;
What is fopen() function?
The fopen() function in C language is used to open a file for reading, writing, or appending. It is defined in the stdio.h library and returns a pointer of type FILE* that is used for further file operations
Syntax:
FILE *fopen(const char *filename, const char *mode);
Parameters:
- filename → Name of the file to be opened (e.g.,
"data.txt"). - mode → Operation mode (read, write, append, etc.).
| File Modes | |
|---|---|
| Mode | Meaning |
| "r" | Opens file for reading. File must exist. |
| "w" | Opens file for writing. Creates new file or overwrites if file exists. |
| "a" | Opens file for appending. Adds data at the end of file. Creates file if not found. |
| "r+" | Opens file for both reading and writing. File must exist. |
| "w+" | Opens file for both reading and writing. Creates new file or overwrites existing one. |
| "a+" | Opens file for reading and appending. Creates new file if not found. |
| "rb","wb","ab" | Open file in binary mode (read/write/append). |
| "rb+","wb+","ab+" | Binary mode with both reading and writing. |
Case Study on Employee Management System Screen Shots
Home Screen
Adding a Record
Displaying Records
Updating a Record
Searching a Record
Searching a Record
Code
#include <stdio.h>
#include <conio.h>
/* user defined structure */
typedef struct Employee {
int eid;
char ena[20],ejob[30];
float esal;
}EMPLOYEE;
/* File Pointer Variable */
FILE *fp;
int getChoice(void);
EMPLOYEE readData();
void addRec(EMPLOYEE);
void showRec(EMPLOYEE);
void delRec(int);
void updateRec(int);
void searchRec(int);
void showtitles(void);
void main() {
int choice,eno;
EMPLOYEE e;
do {
clrscr();
choice = getChoice();
switch( choice ) {
case 1 : e = readData();
addRec(e);
getch();
break;
case 2 : fp = fopen("emp.dat","rb");
if (fp==NULL)
printf("File not found");
else
{
showtitles();
while((fread(&e,sizeof(e),1,fp))!=0) {
showRec(e);
}
fclose(fp);
}
getch();
break;
case 3 :
printf("\n\tEnter Emp. ID to update : ");
scanf("%d",&eno);
updateRec(eno);
getch();
break;
case 4 :
printf("\n\tEnter Emp. ID to delete : ");
scanf("%d",&eno);
delRec(eno);
getch();
break;
case 5 :
printf("\n\tEnter Emp. ID to search : ");
scanf("%d",&eno);
searchRec(eno);
getch();
break;
case 6 :
printf("\n\tThank you, Have a nice day");
getch();
exit(0);
} /* end of switch */
}while(1);
}
int getChoice(void) {
int ch;
printf("\n\t EMPLOYEE DATABSE MAMANGEMENT SYSTEM");
printf("\n\t -----------------------------------");
printf("\n\t 1. Add Record");
printf("\n\t 2. Show Records");
printf("\n\t 3. Update Record");
printf("\n\t 4. Delete Record");
printf("\n\t 5. Search Record");
printf("\n\t 6. Exit");
printf("\n\t Enter your choice : ");
scanf("%d",&ch);
return ch;
}
EMPLOYEE readData(void)
{
EMPLOYEE e;
printf("\n\tEnter Emp. ID : ");
scanf("%d",&e.eid);
printf("\tEnter Emp. Name : ");
scanf("%s",e.ena);
printf("\tEnter Emp. Designation : ");
scanf("%s",&e.ejob);
printf("\tEnter Emp. Basic Salary : ");
scanf("%f",&e.esal);
return e;
}
void addRec(EMPLOYEE e)
{
fp = fopen("emp.dat","ab+");
fwrite(&e,sizeof(e),1,fp);
fclose(fp);
printf("\n\tOne record is added");
}
void showtitles(void)
{
printf("\n\tEmpID | Employee Name | Employee Job | Basic Salary");
printf("\n\t---------------------------------------------------");
}
void showRec(EMPLOYEE e)
{
printf("\n\t%5d|%-16s|%-15s|%10.2f",e.eid,e.ena,e.ejob,e.esal);
}
void updateRec(int eno)
{
EMPLOYEE e,e1;
int flag=0;
long int pos;
fp = fopen("emp.dat","r+b");
if(fp==NULL)
printf("File not found");
else
{
while((fread(&e,sizeof(e),1,fp))!=0)
{
if(eno==e.eid)
{
flag=1;
printf("\n\tRecord Found");
showtitles();
showRec(e);
printf("\n\n\tEnter new data : ");
e1 = readData();
pos=ftell(fp);
fseek(fp,pos-sizeof(e),SEEK_SET);
fwrite(&e1,sizeof(e1),1,fp);
printf("\n\tand updated....");
break;
}
}
fclose(fp);
if(flag==0)
printf("\n\t Record not found to update");
}
}
void delRec(int eno)
{
EMPLOYEE e,e1;
FILE *t;
int flag=0;
fp = fopen("emp.dat","rb");
if(fp==NULL)
printf("\tFile not found");
else
{
t = fopen("temp.dat","wb");
while((fread(&e,sizeof(e),1,fp))!=0)
{
if(e.eid != eno)
{
fwrite(&e,sizeof(e),1,t);
}
else
{
flag=1;
e1 = e;
}
}
fcloseall();
remove("emp.dat");
rename("temp.dat","emp.dat");
if(flag)
{
printf("\n\tRecord found and deleted");
showtitles();
showRec(e1);
}
else
printf("\n\tRecord not found");
}
}
void searchRec(int eno)
{
EMPLOYEE e;
int flag=0;
fp = fopen("emp.dat","rb");
if(fp==NULL)
printf("\tFile not found");
else
{
while((fread(&e,sizeof(e),1,fp))!=0)
{
if(e.eid==eno)
{
flag=1;
break;
}
}
fclose(fp);
if(flag)
{
printf("\n\tRecord found, details are....\n");
showtitles();
showRec(e);
}
else
printf("\n\tRecord not found.");
}
}
Case Study on Student Management System
Screen Shorts
Code
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int getChoice(void);
void addRec(void);
void showRec(void);
void delRec(void);
void insRec(void);
void searchRec(void);
FILE *fp;
typedef struct Student
{
int sno,m1,m2,m3,m4,m5,m6,tot;
char sna[20],res[10],grade;
float avg;
}STUDENT;
int main()
{
int choice;
do
{
choice = getChoice();
switch(choice)
{
case 1 : addRec(); break;
case 2 : showRec(); break;
case 3 : delRec(); break;
case 4 : insRec(); break;
case 5 : searchRec(); break;
case 6 : printf("Thank You, Have a Nice Day");
exit(0);
}
} while (1);
return 0;
}
int getChoice(void)
{
int ch;
printf("\n\t1. Add Record ");
printf("\n\t2. Show Records");
printf("\n\t3. Delete Record");
printf("\n\t4. Insert Record");
printf("\n\t5. Search Record");
printf("\n\t6. Exit");
printf("\n\t Enter your Choice :" );
scanf("%d",&ch);
return ch;
}
void addRec(void)
{
STUDENT st;
printf("Enter Student Number : ");
scanf("%d",&st.sno);
printf("Enter Student Name :" );
scanf("%s",&st.sna);
printf("Enter Telugu Marks : ");
scanf("%d",&st.m1);
printf("Enter English Marks : ");
scanf("%d",&st.m2);
printf("Enter Maths Marks : ");
scanf("%d",&st.m3);
printf("Enter Science Marks : ");
scanf("%d",&st.m4);
printf("Enter Social Marks : ");
scanf("%d",&st.m5);
printf("Enter Hindi Marks : ");
scanf("%d",&st.m6);
st.tot = st.m1+st.m2+st.m3+st.m4+st.m5+st.m6;
st.avg = (float)st.tot/6;
if(st.m1>=35 && st.m2>=35 && st.m3>=35 && st.m4>=35 && st.m5>=35 && st.m6>=20)
{
strcpy(st.res,"Pass");
if(st.avg>=90)
st.grade = 'A';
else if(st.avg>=80)
st.grade='B';
else if(st.avg>=70)
st.grade = 'C';
else if(st.avg>=60)
st.grade='D';
else
st.grade='E';
}
else
{
strcpy(st.res,"Fail");
}
fp = fopen("student.dat","ab+");
fwrite(&st,sizeof(st),1,fp);
fclose(fp);
printf("\nOne record is added successfully.");
}
void showRec(void)
{
STUDENT st;
fp = fopen("student.dat","rb");
if(fp==NULL)
printf("File not found");
else
{
printf("\nSl.No Name of Student Telugu English Maths Science Social Hindi Total Average Result Grade");
printf("\n---------------------------------------------------------------------------------------------");
while( fread(&st,sizeof(st),1,fp) !=0)
{
printf("\n%4d%18s %6d %7d %5d %7d %6d %5d %5d %7.2f %6s %5c",st.sno,st.sna,st.m1,st.m2,st.m3,st.m4,st.m5,st.m6,st.tot,st.avg,st.res,st.grade);
}
fclose(fp);
}
}
void delRec(void)
{
STUDENT st;
int stno,flag=0;
FILE *ptr;
printf("Enter Student Number to delete : ");
scanf("%d",&stno);
fp = fopen("student.dat","rb");
if(fp==NULL)
{
printf("Source File Not found");
}
else
{
ptr = fopen("temp.dat","wb");
while(fread(&st,sizeof(st),1,fp)!=0)
{
if(st.sno!=stno)
fwrite(&st,sizeof(st),1,ptr);
else
flag=1;
}
fclose(fp);
fclose(ptr);
remove("student.dat");
rename("temp.dat","student.dat");
if(flag==1)
printf("Record found and deleted successfully");
}
}
void insRec(void)
{
STUDENT st,t;
int pos,i;
FILE *ptr;
printf("Enter Student Number : ");
scanf("%d",&st.sno);
printf("Enter Student Name :" );
scanf("%s",&st.sna);
printf("Enter Telugu Marks : ");
scanf("%d",&st.m1);
printf("Enter English Marks : ");
scanf("%d",&st.m2);
printf("Enter Maths Marks : ");
scanf("%d",&st.m3);
printf("Enter Science Marks : ");
scanf("%d",&st.m4);
printf("Enter Social Marks : ");
scanf("%d",&st.m5);
printf("Enter Hindi Marks : ");
scanf("%d",&st.m6);
st.tot = st.m1+st.m2+st.m3+st.m4+st.m5+st.m6;
st.avg = (float)st.tot/6;
if(st.m1>=35 && st.m2>=35 && st.m3>=35 && st.m4>=35 && st.m5>=35 && st.m6>=20)
{
strcpy(st.res,"Pass");
if(st.avg>=90)
st.grade = 'A';
else if(st.avg>=80)
st.grade='B';
else if(st.avg>=70)
st.grade = 'C';
else if(st.avg>=60)
st.grade='D';
else
st.grade='E';
}
else
{
strcpy(st.res,"Fail");
}
printf("\nEnter Position to insert : ");
scanf("%d",&pos);
fp = fopen("student.dat","rb");
ptr = fopen("temp.dat","wb");
i=1;
while(fread(&t,sizeof(t),1,fp) !=0)
{
if(i==pos)
fwrite(&st,sizeof(st),1,ptr);
i++;
fwrite(&t,sizeof(t),1,ptr);
}
fclose(fp);
fclose(ptr);
remove("student.dat");
rename("temp.dat","student.dat");
}
void searchRec(void)
{
STUDENT st,t;
int stno,flag=0;
printf("\nEnter student number :" );
scanf("%d",&stno);
fp = fopen("student.dat","rb");
if(fp==NULL)
printf("File does not exist");
else
{
while( (fread(&st,sizeof(st),1,fp))!=0)
{
if(stno==st.sno)
{
t = st;
flag = 1;
break;
}
}
fclose(fp);
printf("Student Details : ");
if(flag==0)
printf("\nNo record found with this student id");
else
{
printf("\nSl.No Name of Student Telugu English Maths Science Social Hindi Total Average Result Grade");
printf("\n---------------------------------------------------------------------------------------------");
printf("\n%5d %18s %6d %7d %5d %7d %6d %5d %5d %7.2f %6s %5c",t.sno,t.sna,t.m1,t.m2,t.m3,t.m4,t.m5,t.m6,t.tot,t.avg,t.res,t.grade);
}
}
} 