Sunday, March 23, 2008

filesystem in linux-text file

#include
#include
#include
/**************MACROS*******************************/
#define SUCCESS 0
#define INVALID_CMD_ERR 1
#define DATA_BASE_FULL_ERR 2
#define ID_NOT_FOUND_ERR 3
#define FAIL 4
#define REC_SIZE 10

/************Structure Type definitions***************/

typedef struct
{
int stId;
char name[20];
int class;
}student_t;

typedef struct
{
char cmdstr[32];
int (*cmdfp)();
}cmd_t;

/*************Function Prototypes**********************/
int display_help();
int display_record();
int add_record();
int del_record();
int mod_record();
int exit_cmd();
void line_to_record(char *lbuf, student_t *ptr);
void strip_newline(char *name);
/*************Global data definitions******************/
FILE *fp;
cmd_t cmds[] =
{
{"add",add_record},
{"disp",display_record},
// {"del",del_record},
// {"mod",mod_record},
{"help",display_help},
{"exit",exit_cmd}
};

int no_of_cmds = sizeof(cmds)/sizeof(cmd_t);


int main()
{
char cmd[100];
int i;
int stat;

printf("the size cmd_t %d size cmds %d",sizeof(cmd_t),sizeof(cmds));
printf("no of cmds %d",no_of_cmds);

printf("\n student record program\n");
fp = fopen("st.txt","r+");
if(!fp)
{
fp = fopen("st.txt","w+");
if(!fp)
{
printf("error in creating st.txt\n");
exit(1);
}
}
display_help();
while(1)
{
printf("enter cmd>");
scanf("%s",cmd);
stat = INVALID_CMD_ERR;
for(i=0; i< no_of_cmds; i++)
{
if(strcmp(cmds[i].cmdstr,cmd) == 0)
{
stat = (*cmds[i].cmdfp)();
break;
}
}
if(stat == INVALID_CMD_ERR)
printf("Invalid command \n enter help to display valid commands\n");
else if(stat != SUCCESS)
printf("command failed with error %d",stat);
else
printf("command sucess and \n");
}
return SUCCESS;
}


int display_help()
{
printf("disp : Display all the records\n");
printf("add : Add a new record\n");
printf("del : Deletes a given record\n");
printf("mod : Modifies a given record\n");
printf("help : Display these help strings\n");
printf("exit : Exits the program\n");

return SUCCESS;
}

int display_record()
{
int length;
student_t st_rec;
char lbuf[100];

rewind(fp);
length = fread(lbuf,1,REC_SIZE,fp);
if(length != REC_SIZE)
{
printf("no record found to printf\n");
return FAIL;
}
while(length == REC_SIZE)
{

line_to_record(lbuf,&st_rec);
if(st_rec.stId != -1)
{
printf("%d\t%s\t%d\n",st_rec.stId,st_rec.name,st_rec.class);
}
length = fread(lbuf,1,REC_SIZE,fp);
}
return SUCCESS;

}
int add_record()
{
int length;
student_t st_rec,temp;
char lbuf[100];

printf("enter id\n");
scanf("%d",&st_rec.stId);

printf("enter name \n");
__fpurge(stdin);
fgets(st_rec.name,40,stdin);
strip_newline(st_rec.name);

printf("enter the class\n");
scanf("%d",&st_rec.class);

rewind(fp);
while(1)
{
length = fread(lbuf,1,REC_SIZE,fp);
if(length != REC_SIZE)
break;
line_to_record(lbuf,&temp);
if(temp.stId == -1)
{
fseek(fp,REC_SIZE* -1,SEEK_CUR);
fprintf(fp,"%4d,%30s,%4d\n",st_rec.stId,st_rec.name,st_rec.class);
return SUCCESS;
}
}
fprintf(fp,"%4d,%30s,%4d\n",st_rec.stId,st_rec.name,st_rec.class);
return SUCCESS;

}
//int del_record();
//int mod_record();

int exit_cmd()
{
fclose(fp);
exit(0);
}
void line_to_record(char *lbuf, student_t *ptr)
{
char *str;

str = strtok(lbuf,",\n");
sscanf(str,"%d",&ptr->stId);
str = strtok(0,",\n");
strcpy(ptr->name,str);
str = strtok(0,",\n");
sscanf(str,"%d",&ptr->class);
}

void strip_newline(char *name)
{
int len;
len = strlen(name);
if(name[len-1] == '\n')
name[len-1] = 0;
}

No comments: