//include files
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"c:/includes.h"
//stores the structures of master and index file
#include"c:/function.cpp" //stores all the functions for the file primitive
/* The main function
input: none
output:none
calleed by: OS.
calls: ADD_RECORD,DELETE_RECORD,MODIFY_RECORD,SEARCH_RECORD,DISPLAY_RECORD
*/
void main(void)
{
int choice;
int status;
int roll_no;
char ch;
clrscr();
printf("Enter the file name:");
scanf("%s",f_name);
printf("enter the index file name:");
scanf("%s",if_name);
ptr=fopen(f_name,"wb");
if(ptr==NULL)
{
printf("file not found");
getch();
exit(0);
}
iptr=fopen(if_name,"wb");
if(iptr==NULL)
{
printf("file not found");
getch();
exit(0);
}
fclose(ptr);
fclose(iptr);
while(1)
{
clrscr();
printf("\n1.add");
printf("\n2.delete");
printf("\n3.modify");
printf("\n4.search");
printf("\n5.display");
printf("\n6.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
status=ADD_RECORD();
if(status==1)
printf("The record has been succesfully added");
else
printf("The record could not be added");
break;
case 2:
printf("Enter the roll_no of the record to delete:");
scanf("%d",&roll_no);
status=DELETE_RECORD(roll_no);
if(status==1)
printf("the record has been succesfully deleted");
else
printf("The record could not be found");
break;
case 3:
printf("Enter the roll_no of the record to modify:");
scanf("%d",&roll_no);
status=MODIFY_RECORD(roll_no);
if(status==1)
printf("The record has been succesfully modified");
else
printf("The record could not be found");
break;
case 4:
printf("Enter the roll_no of the record to search");
scanf("%d",&roll_no);
if(status==1)
printf("The record has been found");
else
printf("The record could be found");
getch();
break;
case 5:
DISPLAY_ALL_RECORD();
getch();
break;
case 6:
getch();
exit(0);
}
printf("Do you want to continue:");
ch=getchar();
if(ch=='n'||ch=='N')
break;
}
getch();
}
/************************************************************
function.cpp
***********************************************************/
/*
the SORT_FILE function
Input:none
Output:1 or -1
called by:ADD_RECORD
calls:none
*/
int SORT_FILE()
{
int size;
int i,j;
is ind, ind_temp;
ss stud;
int flag=0;
iptr = fopen(if_name,"rb+");
if(iptr==NULL)
{
printf("File not opened");
return -1;
}
ptr = fopen(if_name,"rb+");
if(ptr==NULL)
{
printf("File not opened");
return -1;
}
size = 0;
while(fread(&ind,sizeof(is),1,iptr))
size++;
fclose(iptr);
iptr = fopen(if_name,"rb+");
if(iptr==NULL)
{
printf("File not opened");
return -1;
}
for(i=0;i<size;i++)
{
flag = 0;
for(j=0;j<size - (i+1); j++)
{
fseek(iptr,j*sizeof(is),SEEK_SET);
fread(&ind,sizeof(is),1,iptr);
//fseek(iptr,j*size(is),1,SEEK_SET);
fread(&ind_temp,sizeof(is),1,iptr);
if(ind.iroll_no > ind_temp.iroll_no)
{
fseek(iptr,j*sizeof(is),SEEK_SET);
fwrite(&ind_temp,sizeof(is),1,iptr);
fseek(iptr,(j+1)*sizeof(is),SEEK_SET);
fwrite(&ind,sizeof(is),1,iptr);
flag = 1;
}
}
if(flag==0)
break;
}
fclose(iptr);
return 1;
}
/*
The ADD_RECORD function
input:none
output:1 or -1
called by: main
calls: SORT_FILE
*/
int ADD_RECORD()
{
ss stud;
is ind;
long offset;
ptr = fopen(f_name,"rb+");
if(ptr==NULL)
{
printf("File not opened");
return -1;
}
while(fread(&stud,sizeof(stud),1,ptr);
offset = ftell(ptr);
printf("Enter the name of the student");
fflush(stdin);
scanf("%s",&stud.name);
printf("Enter the roll no. of the student");
fflush(stdin);
scanf("%d",&stud.roll_no);
printf("Enter the age of the student");
fflush(stdin);
scanf("%d",&stud.age);
printf("Enter the marks of the student");
fflush(stdin);
scanf("%d",&stud.mark);
fwrite(&stud,sizeof(stud),1,ptr);
fclose(ptr);
iptr = fopen(if_name,"ab+");
if(iptr==NULL)
{
printf("File not opened");
return -1;
}
ind.iroll_no = stud.roll_no;
ind.offset = offset;
ind.flag = 1;
fwrite(&ind,sizeof(ind),1,iptr);
fclose(iptr);
SORT_FILE();
return 1;
}
/*
the DELETE_RECORD function
input:roll_no
output:either -1 or 1
called by:main
calls:none
*/
int DELETE_RECORD(int roll_no)
{
is ind;
int flag = 0;
iptr = fopen(if_name,"rb+");
if(iptr==NULL)
{
printf("File not opened");
return -1;
}
while(fread(&ind,sizeof(is),1,iptr));
{
if(ind.iroll_no == roll_no)
{
flag = 1;
break;
}
}
if (flag == 1)
{
ind.flag = -1;
fseek(iptr,-(long)sizeof(is),SEEK_CUR);
fwrite(&ind,sizeof(is),1,iptr);
fclose(iptr);
return 1;
}
fclose(iptr);
return -1;
}
/*
The MODIFY_RECORD function
Input:roll_no
output:1
called by:main
calls:none
*/
int MODIFY_RECORD(int roll_no)
{
is ind;
ss stud;
int flag = 0;
iptr = fopen(if_name,"rb+");
if(iptr == NULL)
{
printf("File not opened ");
return -1;
}
while(fread(&ind,sizeof(is),1,iptr))
{
if(ind.iroll_no == roll_no)
{
flag = 1;
break;
}
}
fclose(iptr);
if(flag == 1)
{
ptr = fopen(f_name,"rb+");
if(ptr == NULL)
{
printf("File not opened");
return -1;
}
fseek(ptr,ind.offset,SEEK_SET);
fread(&stud,sizeof(stud),1,ptr);
printf("Enter the name of the student");
fflush(stdin);
scanf("%s",stud.name);
printf("Enter the age of the student");
fflush(stdin);
scanf("%d",&stud.age);
printf("Enter the marks of the student");
fflush(stdin);
scanf("%d",&stud.mark);
fseek(ptr,-(long)sizeof(stud),SEEK_CUR);
fwrite(&stud,sizeof(stud),1,ptr);
fclose(ptr);
return 1;
}
return -1;
}
/*
The SEARCH_RECORD function
input:roll_no
output:if record foud then 1
called by:main
calls:none
*/
int SEARCH_RECORD(int roll_no)
{
is ind;
ss stud;
iptr = fopen(if_name,"rb+");
if(iptr == NULL)
{
printf("File not opened");
return -1;
}
while(fread(&ind,sizeof(is),1,iptr))
{
if(ind.iroll_no == roll_no && ind.flag == 1)
{
fclose(iptr);
return 1;
}
}
fclose(iptr);
return -1;
}
/*
The DISPLAY_ALL_RECORD function
input:none
output:-1 if no other record otherwise 1
called by:main
calls:none
*/
int DISPLAY_ALL_RECORD()
{
is ind;
ss stud;
iptr = fopen(if_name,"rb+");
if(iptr == NULL)
{
printf("file not opened");
return -1;
}
while(fread(&ind,sizeof(is),1,iptr))
{
if(ind.flag == 1)
{
fseek(ptr,ind.offset,SEEK_SET);
fread(&stud,sizeof(stud),1,ptr);
printf("\n\n\nRoll : %d",stud.roll_no);
printf("\nName : %s",stud.name);
printf("\nAge : %d",stud.age);
printf("\nMarks : %d",stud.mark);
}
}
fclose(iptr);
fclose(ptr);
return 1;
}
/******************************************************************
include.h
******************************************************************/
typedef struct student
{
int roll_no;
char name[20];
int age;
int mark;
}ss;
typedef struct index
{
int iroll_no;
long offset;
int flag;
}is;
FILE *ptr=NULL;
FILE *iptr=NULL;
char f_name[20]; //master file
char if_name[20]; //index file
danieldane 0 Newbie Poster
danieldane 0 Newbie Poster
L7Sqr 227 Practically a Master Poster
danieldane 0 Newbie Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
danieldane 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.