how can we update or delete the perticual record by using c language?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnagJohari
    New Member
    • May 2010
    • 96

    how can we update or delete the perticual record by using c language?

    I have a problem to update or delete the record from that file. so i request please send me the code of updating & deleting the record from the file......
    please guys help me
    thank you
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    R u maintaining the file like DB?
    Can you give more details so that people can help u with ideas.

    Raghu

    Comment

    • AnagJohari
      New Member
      • May 2010
      • 96

      #3
      Originally posted by gpraghuram
      Hi,
      R u maintaining the file like DB?
      Can you give more details so that people can help u with ideas.

      Raghu
      ya you can say this i enter the information in the field
      now i want to delete perticular record which we enter bt using fprintf() & fscanf() functions
      ok i send you a code check it
      Code:
      #include<conio.h>
      #include<stdio.h>
      void main()
      FILE *fp;
      float price,value;
      int number,quantity,i;
      char item[10],filename[10];
      printf("input file name\n");
      scanf("%s",filename);
      fp=fopen(filename,"w");
      printf("Item name  Number  Price  Quantity\n");
      for(i=1;i<=3;i++)
      {
      fscanf(stdin,"%s %d %f %d",item,&number,&price,&quantity);
      fprintf(fp,"%s %d %.2f %d",item,number,price,quantity);
      }
      fclose(fp);
      fprintf(stdout,"\n\n");
      fopen(filename,"r");
      printf("Item name  Number  Price  Quantity  value\n");
      for(i=1;i<=3;i++)
      {
      fscanf(fp,"%s %d %f %d",item,&number,&price,&quantity);
      value=price*quantity;
      fprintf(stdout,"%-8s %7d %8.2f %8d 11.2%f\n",item,number,price,quantity,value);
      }
      fclose(fp);
      }
      now i want to delete any of one record which i enter from the above code please help me by making delete function so that i can delete a perticualar recode & also make a function of updation so that i can update any sort of record
      i hope now you are getting what i want to tell you>

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Your database file is implemented as a text file with each line being a different record. There is no assurance that all records are the same length. You cannot accomplish delete or update "in-place". You will have to accomplish these functions by making a modified copy of the database file.

        Code:
        delete algorithm:
        Repeat for each line in the input file {
           Read the next line from the input file.
           If this is not the line to be deleted {
              Write the line to the output file.
              }
           }
        Code:
        update algorithm:
        Repeat for each line in the input file {
           Read the next line from the input file.
           If this is not the line to be updated {
              Write the line to the output file.
              }
           Else {
              Modify the line as per the desired update.
              Write the modified line to the output file.
              }
           }
        The last step for both cases is to replace the input file with the output file.

        Comment

        • nike111
          New Member
          • Jun 2009
          • 15

          #5
          You cant delete as such from a text file..
          try to maintain a fixd length for every record...
          and search for the element you want to remove, leaving that, copy all the other records to another file "temp"
          see if all records are copied , delete the existing one and rename the "temp" file

          Comment

          • AnagJohari
            New Member
            • May 2010
            • 96

            #6
            Really tnx to give me a reply i try to understand this

            Comment

            • AnagJohari
              New Member
              • May 2010
              • 96

              #7
              the one more problem is ocuured suppose i intialize 100 records in a file by giving null values or blankspaces or
              0 for integer values
              now i want to input 10 records inplace of intialize values
              i m able to intialize the values in record
              but not able to insert the value in place of intialize values
              i send you a code plz check it & suggest me
              i try very hard to do this but not succeed.
              Code:
              #include<conio.h>
              #include<stdio.h>
               void display();
              FILE *fp;
              float cost,value;
              int record,quantity,i;
              char ToolName[10],filename[10];
              void main()
              {
              
              printf("input file name\n");
              scanf("%s",filename);
              fp=fopen(filename,"r+");
              printf("Item name  Number  Price  Quantity\n");
              for(i=1;i<=3;i++)
              { ToolName[i]='0',record=0,cost=0,quantity=0;
              //fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
              fprintf(fp,"%s %d %.2f %d",ToolName,record,cost,quantity);
              
              }
              
              display();
              rewind(fp);
              fprintf(stdout,"%s %d %f %d \n",ToolName,record,cost,quantity);
              while((fp,"%s %d %f %d",ToolName,&record,&cost,&quantity)!=0);
              {
              fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
              fprintf(stdout,"%s %d %f %d \n",ToolName,record,cost,quantity);
              }
              fclose(fp);
              display();
              /*fp=fopen(filename,"r+");
              rewind(fp);
              while(fscanf(fp,"%s %d %f %d",ToolName,&record,&cost,&quantity)!=0);--->problem is here in while loop plz see this problem
              {
              fscanf(stdin,"%s %d %f %d",ToolName,&record,&cost,&quantity);
              fprintf(fp,"%s %d %.2f %d",ToolName,record,cost,quantity);
              }
              display();*/
              getch();
              }
               void display()
              {int i;
              fprintf(stdout,"\n\n");
              fopen(filename,"r");
              printf("Item name  Number  Price  Quantity  value\n");
              for(i=1;i<=3;i++)
              {
              fscanf(fp,"%s %d %f %d",ToolName,&record,&cost,&quantity);
              //value=cost*quantity;
              fprintf(stdout,"%s %7d %f %d \n",ToolName,record,cost,quantity);
              }
              fclose(fp);
              }

              Comment

              Working...