How to update and delete particular record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svsteja
    New Member
    • Oct 2012
    • 1

    How to update and delete particular record

    please say how to update a particular record and delete particular record from the following code:

    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    struct cust
    {
    	char name[40];
    	char details[40];
    };
    int main()
    {
    	struct cust c;
    	FILE*fp;
    	char custname[20],choice,s;
    	
    	while(1)
        {
           
            printf("1.enter customer details.\n");
            printf("2. search for customer details .\n");
            printf("3. exit.\n");
            printf("enter ur choice\n");
            choice=getchar();
            switch(choice)
    			{
                case '1':
                        fp=fopen("cust.txt","ab");
                        while (1)
                        {
                          printf("enter customer name:\n");
                          scanf("%s",c.name);
                          fflush(stdin);
                          printf("\nenter details:");
                          scanf("%s",c.details);
                          fwrite(&c,sizeof(c),1,fp);
                          fflush(stdin);
                          printf("\n\ndo u wnt to continue(Y/N)");
                          scanf("%s",&s);
                          if(s=='n'||s=='N')
                                break;
                        }
                        fclose(fp);
                        break;
    	 case '2':
                        fp=fopen("cust.txt","rb");
                        printf("enter customer name:\n");
                        scanf("%s",custname);
                        while(fread(&c,sizeof(c),1,fp))
                        {
                            if(strcmp(custname,c.name)==0)
                            {
    							printf("\n\t %s\t %s",c.name,c.details);
                                getchar();
                                break;
                            }
    						else
    						{
    							printf("no details founnd:\n");
    						}
                        }
                        fclose(fp);
                        getchar();
                        break;
                case '3':
                        exit(0);
            }
        }
    	getchar();
    }
    Last edited by Meetee; Oct 31 '12, 11:58 AM. Reason: please use code tags <code/> around your code
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are using a file. What does delete mean? The file has the records already written.

    Do you intend to copy file file up to the deleted record and then skip the deleted record and then copy the file back to the original? Assuming you can find the record to be deleted.

    Do you intend to overwrite the deleted record with coded characters, like 032 or 003 and then access the file with functions that are smart enough to skip over these "deleted" characters. This would also assume a compress function to squeeze the deleted characters out of the file usually by making copy of the file by skipping over these characters.

    Does your file have the correct format? As in is it a text file where all records contain characters or is it a compressed file where you have embedded start/stop characters for each record?

    All of this means that you cannot process a file unless you know how it was written and what rules were used to put the data in the file.

    Comment

    Working...