Friday 9 September 2016

PROBLEM TITLE:-
How to create fixed length storage program in C language without using another file?
or
How to delete, update in a file without using another file?
or
How to perform basic operations on file(using single file only)?

Solution:-

#include<conio.h>
#include<stdio.h>
FILE *fp;
struct record
{
 char name[20];
 int rno;           //rollno
 int lno;           //recordno or line no

} r;

void insert()
{       int count=0,wti=0,wtip=0,i;    //wti=where to insert
 fp=fopen("try.txt","r");       //wtip=wti pointer
 if(fp==NULL)
 {
 puts("cannot open file");
  exit(1);
 }
 while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
 {
    count++;
  if(r.lno==0&&wtip==0)
  {
   wti=count;
   wtip=1;
  }

  //printf("\t%d \t%d \t%s\n",r.lno,r.rno,r.name);
 }
 //struct record *p=(struct record *)malloc(sizeof(struct record)*count);

 fclose(fp);
 i=0; wtip=0;
 fp=fopen("try.txt","r");
 if(count>=1&wti!=0)
 {       struct record r1;
  struct record *p=(struct record *)malloc(sizeof(struct record)*count);
  while(fscanf(fp,"%d %d %s",&r1.lno,&r1.rno,r1.name)!=EOF)
  {       if(i==wti-1&&wtip==0)
   {
    printf("please enter name and rollno:-");
    scanf("%s %d",r.name,&r.rno);
    r.lno=wti;
    p[i++]=r;
    wtip=1;
   }
   else
   p[i++]=r1;
  }

  fclose(fp);
  fp=fopen("try.txt","w+");
  for(i=0;i<count;i++)
  {
   fprintf(fp,"%d %d %s\n",p[i].lno,p[i].rno,p[i].name);
  }
  fclose(fp);

 }
 else
 {
  fp=fopen("try.txt","a");
  printf("\nPlease enter name and rollno:");
  scanf("%s %d",r.name,&r.rno);
  r.lno=count+1;
  fprintf(fp,"%d %d %s\n",r.lno,r.rno,r.name);
  //struct record *d=(struct record *)malloc(sizeof(struct record)*t);

 fclose(fp);
 }
}

void display()
{
 fp=fopen("try.txt","r");
 if(fp==NULL)
 {
  puts("cannot open file");
  exit(1);
 }
        printf("\tRECORD_NO \tRollNO \t\tNAME\n");
        printf("\t===========    =============   ==============\n");
  while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
  {
   printf("\t%d  \t\t%d  \t\t%s\n",r.lno,r.rno,r.name);
  }
        printf("\t===========    =============   ==============\n\n");

 fclose(fp);

}

void del()
{
int count=0,choice,i;
 fp=fopen("try.txt","r");
 if(fp==NULL)
 {
 puts("cannot open file");
 exit(1);
 }
 while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
 {
  count++;
  //printf("\t%d \t%d \t%s\n",r.lno,r.rno,r.name);
 }
  printf("please enter where you want to delete:-");
  scanf("%d",&choice);

 fclose(fp);
 fp=fopen("try.txt","r");
 i=0;
 if(count>=1&&choice<=count&&choice>0)
 {       //struct record r1;
  struct record *p=(struct record *)malloc(sizeof(struct record)*count);
  while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
  {
   p[i++]=r;
  }
  for(i=0;i<count;i++)
  {
   if(p[i].lno==choice)
   {
    r.lno=NULL;
    r.rno=NULL;
    r.name[0]='#';
    r.name[1]=NULL;
    p[i]=r;
   }
  }
  fclose(fp);
  fp=fopen("try.txt","w+");
  for(i=0;i<count;i++)
  {
   fprintf(fp,"%d %d %s\n",p[i].lno,p[i].rno,p[i].name);
  }
  fclose(fp);

 }
 else
 {
  printf("invalid record choice.....\n") ;
 }
}


void update()
{
int count=0,choice,i;
 fp=fopen("try.txt","r");
 if(fp==NULL)
 {
 puts("cannot open file");
 exit(1);
 }
 while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
 {
  count++;
  //printf("\t%d \t%d \t%s\n",r.lno,r.rno,r.name);
 }
  printf("please enter the location where you want to update:-");
  scanf("%d",&choice);

 fclose(fp);
 fp=fopen("try.txt","r");
 i=0;
 if(count>=1&&choice<=count&&choice>0)
 {       //struct record r1;
  struct record *p=(struct record *)malloc(sizeof(struct record)*count);
  while(fscanf(fp,"%d %d %s",&r.lno,&r.rno,r.name)!=EOF)
  {
   p[i++]=r;
  }
  for(i=0;i<count;i++)
  {
   if(p[i].lno==choice)
   {
    printf("please insert name and rollno:=");
    scanf("%s %d",r.name,&r.rno);
    r.lno=choice;
    p[i]=r;
    break;
   }
  }
  fclose(fp);
  fp=fopen("try.txt","w+");
  for(i=0;i<count;i++)
  {
   fprintf(fp,"%d %d %s\n",p[i].lno,p[i].rno,p[i].name);
  }
  fclose(fp);

 }
 else
 {
  printf("invalid record choice.....\n") ;
 }

 }

int main()
{
int ch;
clrscr();
fp=fopen("try.txt","r");       
 if(fp==NULL)
 {
 fp=fopen("try.txt","w");
 fclose(fp);
 }
 fclose(fp);
while(1)
{
 fflush(stdin);
 printf("Please enter your choice: among following:--\n");
 printf("1.Insert\n2.Display\n3.Delete\n4.Update\n5.Exit\n your choice:=");
 scanf("%d",&ch);
 clrscr();
 switch(ch)
 {
  case 1:insert();        break;
  case 2:display();             break;
  case 3:del();                       break;
  case 4:update();                          break;
  case 5: exit(0);
  default:printf("please enter right choice");

 }

}
//getch();
//return 0;
}

2 comments:

  1. Thankyou very much, its really halpful for me.

    ReplyDelete
  2. Thank you dear ... really helpful ......

    ReplyDelete