Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define SUCCESS 0
#define DATA_BASE_ERR 1
#define ID_NOT_FOUND_ERR 2
#define REC_SIZE 28
typedef struct
{
int studentId;
char name[20];
int phoneNum;
}student_t;
void strip_newline(char *name);
void disp_help();
int addNewStudentRec(FILE *fp,student_t *pS);
int dispAllStudentRecs(FILE *fp);
#if 0
int deleteStudentRec(FILE fp,int studentId);
#endif
int main()
{
int option = 0,fd = 0,id = 0;
student_t nRec;
FILE *fp;
memset(&nRec,0,sizeof(nRec));
fp = fopen("student.txt","r+");
if (!fp)
{
printf("Creating the file \n");
fp = fopen("student.txt","w+");
if (!fp)
{
printf("Could not create file \n");
exit(1);
}
}
disp_help();
while(1)
{
printf("\n Enter Option: ");
__fpurge(stdin);
scanf("%d",&option);
switch(option)
{
case 1:
dispAllStudentRecs(fp);
break;
case 2:
printf("Enter id name and phone \n");
scanf("%d",&nRec.studentId);
__fpurge(stdin);
fgets(nRec.name,20,stdin);
strip_newline(nRec.name);
scanf("%d",&nRec.phoneNum);
if(addNewStudentRec(fp,&nRec) != SUCCESS)
printf("Record add operation failed \n");
break;
#if 0
case 3:
printf("Enter id of record to delete :");
scanf("%d",&id);
if(deleteStudentRec(fd,id)!= SUCCESS)
printf("Record delete operation failed \n");
break;
#endif
case 4:
printf("Good bye \n");
return SUCCESS;
default:
printf("Invalid option.Use the following \n");
disp_help();
break;
}//switch
}//while
return SUCCESS;
}
void strip_newline(char *name)
{
int len;
len = strlen(name);
if (name[len-1] == '\n')
name[len-1] = 0;
}
void disp_help()
{
printf("Enter 1 to display all records \n");
printf("Enter 2 to add a record \n");
printf("Enter 3 to delete a record \n");
printf("Enter 4 to exit \n");
}
void line2rec(char *lbuf,student_t *pr)
{
char *str;
int len;
str = (char *)strtok(lbuf,",\n");
sscanf(str,"%d",&pr->studentId);
str = (char *)strtok(0,",\n");
len = strlen(str);
str[len] = '\0';
strcpy(pr->name,str);
printf("before strtok line 2 rec %s\n",pr->name);
str = (char *)strtok(0,",\n");
sscanf(str,"%d",&pr->phoneNum);
printf("after strtok line 2 rec %s\n",pr->name);
}
int dispAllStudentRecs(FILE *fp)
{
int len;
student_t rec;
char lbuf[100];
memset(&rec,0,sizeof(rec));
memset(&lbuf,0,sizeof(lbuf));
fseek(fp,0,SEEK_SET);
len = fread(&lbuf,1,REC_SIZE,fp);
if (len != REC_SIZE)
{
printf("No record to print \n");
return ;
}//if
while(len == REC_SIZE)
{
line2rec(lbuf,&rec);
if (rec.studentId != -1)
{
printf("%d,%20s,%d\n",rec.studentId,rec.name,rec.phoneNum);
}//if
memset(&lbuf,0,sizeof(lbuf));
memset(&rec,0,sizeof(rec));
len = 0;
len = fread(&lbuf,1,REC_SIZE,fp);
}//while
return SUCCESS;
}//dispAllStudentRecs
int addNewStudentRec(FILE *fp,student_t *rec)
{
int len;
student_t trec;
char lbuf[100];
fseek(fp,0,SEEK_SET);
while(1)
{
len = fread(lbuf,1,REC_SIZE,fp);
if (len != REC_SIZE)
{
break;
}
line2rec(lbuf,&trec);
if(trec.studentId == -1)
{
fseek(fp,REC_SIZE * -1,SEEK_CUR);
fprintf(fp,"%d,%20s,%d\n",rec->studentId,rec->name,rec->phoneNum);
return SUCCESS;
}
}
fprintf(fp,"%d,%20s,%d\n",rec->studentId,rec->name,rec->phoneNum);
return SUCCESS;
}