Thread: delete a record,urgent!!!!!!

  1. #1
    Unregistered
    Guest

    delete a record,urgent!!!!!!

    i am writing to delete a specific record in a file and how can i do that???
    User will enter their reference number, if it is same as that in file, detail will be printed.After that, how can i delete that record??????
    pls help!!


    in=fopen("customer.dat","a");
    if (in==NULL)
    printf("File could not opened !\n");

    printf("\t\tPlease enter Reservation Number for deletion\n"
    printf("\t\tYour reservation number : ");
    scanf("%d",&input);


    while(!feof(in))
    {
    fscanf(in,"%d%d%d",&ref,&classno,&count);
    if (input==ref)
    {
    printf("\t\t%5d%15d%8d\n",ref,classno,count);
    printf("\n\t\t\t\tDelete (Y/N) : ");
    scanf("%d",&ans4);

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Just check to see if they entered 'Y' and then you have two options.

    1. Set the record values to a pre-set identifier like '-1' so that you know it has been deleted. Then just set your options that handle the records to skip the ones with that value.

    2. If you want to total remove it, use a for loop and start at the deleted record and replace each record with the one after it.

    If you need more help, pick which one you want to do and post the rest of the code. I won't be able to look at it until tomorrow afternoon, but someone else may get to it before then.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could read the complete file into memory (providing it's not too big), process the data, then write the complete file back to disk.

    Something like:
    Code:
    /* psuedo */
    Get file size.
    malloc memory sizeof file size.
    fread file into memory.
    process data (maybe use strtok to split it up).  Maybe load data lines into a new array/linklist to make them easier to handle.
    Overwrite the file with the new data.
    free memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    As it is you're reading a record from the file, testing it for equality with the user input, and then asking if the user wants to delete it. A simple change is to save the record to a temporary file immediately if it fails the test, and if it does not fail the test you either save the record or ignore it based on user input. If the user wants to delete the record you simply don't write it to the temporary file. Copy the temporary file to the original file and you're done.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM