#include
#include
#include
#define SUCCESS 0
#define DATA_FULL 1
#define ID_NOT_FOUND 2
#define EMPTY_RECORD 3
#define MAX_RECORD 100
typedef struct
{
int stid;
char name[20];
int class;
}student_t;
#define RECORD_SIZE sizeof(student_t)
//int fd;
void display_help(void);
int file_open(void);
int display_record(int );
int add_record(int ,student_t *);
int delete_record(int ,int );
int search_record(int ,int );
int modify_record(int ,student_t *);
int main()
{
int option,status,fd,id;
student_t st,*pst;
pst = &st;
while(1)
{
display_help();
printf("select the option\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("enter the student record\n");
printf("enter the student id\n");
scanf("%d",&pst->stid);
printf("enter the student name\n");
scanf("%s",pst->name);
printf("enter the student class\n");
scanf("%d",&pst->class);
fd = file_open();
status = add_record(fd,pst);
if(status == SUCCESS)
printf("adding into record is success \n");
else
printf("adding into record is fail \n");
break;
case 2:
fd = file_open();
status = display_record(fd);
if(status == SUCCESS)
printf("display record is success \n");
else if(status = EMPTY_RECORD)
printf("no record to display \n");
break;
case 3:
printf("enter the student record for modification \n");
printf("enter the student id\n");
scanf("%d",&pst->stid);
printf("enter the modify student name\n");
scanf("%s",pst->name);
printf("enter the modify student class\n");
scanf("%d",&pst->class);
status = modify_record(fd,pst);
if(status == SUCCESS)
printf("modify record is success \n");
else
printf("modify record is fail \n");
break;
case 4:
fd = file_open();
printf("enter the student id\n");
scanf("%d",&id);
status =delete_record(fd,id);
if(status == SUCCESS)
printf("delete record is success \n");
else
printf("delete record is fail \n");
break;
/* case 5:
status = search_record(fd,stId);
if(status == SUCCESS)
printf("search record is success \n");
else
printf("search record is fail \n");
break;*/
case 6:
exit(0);
default:
printf("enter the worng option\n");
}
}
}
int file_open(void)
{
int fd;
fd = open("ravi.rec",O_RDWR);
if(fd < 0)
{
printf("creating file ....\n");
fd = open("ravi.rec",O_RDWR|O_CREAT,0600);
if(fd < 0)
{
printf("error in file creating\n");
}
}
return fd;
}
void display_help(void)
{
printf("1 add record\t 2 display record\t3 modify record\n4 delete record\t 5 search record\t 6 exit\n");
}
int display_record(int fd)
{
int length;
student_t rec;
lseek(fd,0,SEEK_SET);
length = read(fd,&rec,RECORD_SIZE);
if(length != RECORD_SIZE)
{
printf("no records to print\n");
close(fd);
return(EMPTY_RECORD);
}
printf("ID NAME CLASS\n");
printf("---------------------------\n");
while(length == RECORD_SIZE)
{
if(rec.stid != -1)
printf("%d\t %s\t %d\n",rec.stid,rec.name,rec.class);
length = read(fd,&rec,RECORD_SIZE);
}
close(fd);
return(SUCCESS);
}
int add_record(int fd,student_t *new)
{
int length;
student_t rec,temp_rec;
lseek(fd,0,SEEK_SET);
while(1)
{
length = read(fd,&temp_rec,RECORD_SIZE);
if(length != RECORD_SIZE)
break;
printf("if record is empty\n");
if(temp_rec.stid == -1)
{
lseek(fd,RECORD_SIZE * -1,SEEK_CUR);
write(fd,new,RECORD_SIZE);
close(fd);
return SUCCESS;
}
}
write(fd,new,RECORD_SIZE);
close(fd);
return SUCCESS;
}
int delete_record(int fd ,int id )
{
int length;
student_t temp_rec;
lseek(fd,0,SEEK_SET);
while(1)
{
length = read(fd,&temp_rec,RECORD_SIZE);
if(length != RECORD_SIZE)
break;
printf("if record is empty\n");
if(temp_rec.stid == id)
{
lseek(fd,RECORD_SIZE * -1,SEEK_CUR);
temp_rec.stid = -1;
write(fd,&temp_rec,RECORD_SIZE);
close(fd);
return SUCCESS;
}
}
close(fd);
return ID_NOT_FOUND;
}
//int search_record(int ,int );
int modify_record(int fd,student_t *modify)
{
int length;
student_t temp_rec;
lseek(fd,0,SEEK_SET);
while(1)
{
length = read(fd,&temp_rec,RECORD_SIZE);
if(length != RECORD_SIZE)
break;
if(temp_rec.stid == modify->stid)
{
printf("modifing record\n");
lseek(fd,RECORD_SIZE * -1,SEEK_CUR);
write(fd,modify,RECORD_SIZE);
close(fd);
return SUCCESS;
}
}
return FAIL;
}

No comments:
Post a Comment