Saturday, May 10, 2008

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;
}

File systems in linux-binary file

#include
#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;
}

Friday, January 25, 2008

find armstrong numbr

/* pgr t o find armstrong numbr
written by sekhar
written on 28/08/07
*/
#include
#include
main()
{
int x,value,z,y=0;
scanf("%d",&value);
z=value;
while(z!=0)
{

x=z%10;
y=(x*x*x)+y;
z=z/10;
}
if(y==value)
printf("given no is amstrong number");
}

linked lists in data structures

/*pgr to use to linked lists in data structures
written by sekhar
written on 22/07/08 */

#include
typedef struct linkedlist
{
int rollno;
char name[20];
int marks;
char remarks[20];
struct linkedlist *next;
}stu_t;
stu_t *insert(stu_t *);
void *display(stu_t *);
main()
{
int option,status;
stu_t *head;
head=(stu_t *)malloc(sizeof(stu_t));

while(1)
{
puts("enter the option 1: insert 2:delete 3:update 4:sort 5:display 6:search 7:exit");
scanf("%d",&option);
switch(option)
{
case 1:
head=insert(head);
/* if(status==0)
printf("operation is success");
else
printf("operation is denied");*/
break;

case 5:
display(head);
break;

case 6:
exit();
}
}
}
stu_t *insert(stu_t *head)
{
int option,b4number;
char value='y';
stu_t *new,*temp,*list;
list=head;
temp=head;
puts("enter the option 1:bottom 2:middle 3:top");
scanf("%d",&option);
switch(option)
{
case 1:
while(value=='y')
{
printf("enter the details");
scanf("%d%s%d%s",&list->rollno,list->name,&list->marks,list->remarks); new=(stu_t *)malloc(sizeof(stu_t));
list->next=new;
list=new;
puts("enter the option you want to add another record y:yes n:no");
__fpurge(stdin);
scanf("%c",&value);
}
if(value!='y')
list->next=0;
return head;

case 2:
while(value=='y')
{
puts("enter the position where u want to enter");
scanf("%d",&b4number);
puts("enter the new fields");
new=(stu_t *)malloc(sizeof(stu_t));
scanf("%d%s%d%s",&new->rollno,new->name,&new->marks,new->remarks);
while(temp->next)
{
if(temp->rollno==b4number)
{
new->next=temp->next;
temp->next=new;
}
temp=temp->next;
}
puts("if you want ot add another option please press 'y'");
__fpurge(stdin);
scanf("%c",&value);
}

case 3:
while(value=='y')
{
puts("enter the fields");
new=(stu_t *)malloc(sizeof(stu_t));
scanf("%d%s%d%s",&new->rollno,new->name,&new->marks,new->remarks);
new->next=head;
head=new;
puts("if you want ot add another option please press 'y'");
__fpurge(stdin);
scanf("%c",&value);
}
return head;

}
}


void *display(stu_t *head)
{
while(head->next!=0)
{
printf("%d\t%s\t%d\t%s\n",head->rollno,head->name,head->marks,head->remarks);
head=head->next;
}
}

linked list operation basic program

/* pgr to write linked list operation in the linked list
written by sekhar
written on 21/08/07
modified on 21/08/07 */

#include
//#include
typedef struct linkedlist
{
int number;
struct linkedlist *next;
}num_t;

void create(num_t *);
void print(num_t *);
num_t *insert(num_t *);
num_t *find(num_t *,int);
main()
{
num_t *head;
head=(num_t *)malloc(sizeof(num_t));
create(head);
//print(head);
head=insert(head);
print(head);
}

void create(num_t *list)
{
printf("input a number\n");
printf("type -999 at a end ");
scanf("%d",&list->number);
if(list -> number==-999)
{
list->next=0;
}
else
{
list->next=(num_t *)malloc(sizeof(num_t));
create(list ->next);
}
return;
}

void print(num_t *list)
{
if(list->next!=0)
{
printf("%d -->",list->number);
print(list->next);
}
return;
}

num_t *insert(num_t *head)
{
num_t *n1;
int x;
int key;
num_t *new;
num_t *temp;

temp = head;
printf("enter the new value");
scanf("%d",&x);
printf("enter the key value");
scanf("%d",&key);

if(head->number==key)
{
new=(num_t *)malloc(sizeof(num_t));
new->number=x;
new->next=head;
head=new;
return(head);
}

while(temp->next)
{
if(temp-> number == key)
{
new=(num_t *)malloc(sizeof(num_t));
new->number=x;
new->next=temp->next;
temp->next=new;
return head;
}
temp = temp->next;
}
}
/*else
{
n1=find(head,key);
if(n1==0)
printf("key is not found");
else
{
new=(num_t *)malloc(sizeof(num_t));
new->number=x;
new->next=n1->next;
n1->next=new;
}
}
return(head);
}


num_t *find(num_t *list,int key)
{
if(list->number==key)
return list;
else
if(list->next==0)
return 0;
else
find(list->next,key);
}*/

single Linked list operations

/* pgr to write linked list operation in the linked list
written by sekhar
written on 21/08/07
modified on 21/08/07 */

#include
//#include
typedef struct linkedlist
{
int number;
struct linkedlist *next;
}num_t;

void create(num_t *);
void print(num_t *);
num_t *insert(num_t *);
num_t *find(num_t *,int);
main()
{
num_t *head;
head=(num_t *)malloc(sizeof(num_t));
create(head);
//print(head);
head=insert(head);
print(head);
}

void create(num_t *list)
{
printf("input a number\n");
printf("type -999 at a end ");
scanf("%d",&list->number);
if(list -> number==-999)
{
list->next=0;
}
else
{
list->next=(num_t *)malloc(sizeof(num_t));
create(list ->next);
}
return;
}

void print(num_t *list)
{
if(list->next!=0)
{
printf("%d -->",list->number);
print(list->next);
}
return;
}

num_t *insert(num_t *head)
{
num_t *n1;
int x;
int key;
num_t *new;

printf("enter the new value");
scanf("%d",&x);
printf("enter the key value");
scanf("%d",&key);

if(head->number==key)
{
new=(num_t *)malloc(sizeof(num_t));
new->number=x;
new->next=head;
head=new;
}
else
{
n1=find(head,key);
if(n1==0)
printf("key is not found");
else
{
new=(num_t *)malloc(sizeof(num_t));
new->number=x;
new->next=n1->next;
n1->next=new;
}
}
return(head);
}
num_t *find(num_t *list,int key)
{
if(list->next->number==key)
return list;
else
if(list->next->next==0)
return 0;
else
find(list->next,key);
}