Thread: Deleting a Record from an input file

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Deleting a Record from an input file

    I'm having some trouble understanding what I have to code for removing a product record from the file (having all it's field information be deleted). The code that I currently have for that section is not working. It asks the user to enter the Product ID of the file they wish to delete and then the program should find the record and delete it. I would like it to be able to move all the records in the file up 1 position from the position of the file that was deleted so that there are no blanks in the file.

    I've attached my code since it's a little different because it involves classes.

    It's the last part that I need to get working on this program.

    Any help or advice would be greatly appreciated on this subject. Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It sounds like you're trying to simply "remove" a part of the file. You need to read the whole file into a block of memory, read all the data you want kept into a separate block of memory (perhaps in a loop, just skip the stuff you want deleted), and then write that new block of data to the file.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is file-mapping.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    What is file-mapping and how is it done?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    73
    Alright I'm close to getting the right solution for deleting a record from a file. It is finding the file to delete but instead of completely getting rid of the record and not seeing it anymore, it is displaying the deleted record as a bunch of weird characters in the file.

    Here is my code for the section on deleting a record:

    Code:
    // delete a product from the inventory
             case 'd':
                system("cls");
                cout << "Enter product ID number of the product you 
                wish to delete: ";
                cin >> delnum;
                cin.ignore();
    
                // for all records in the file
                for (j = 0; j <= p; j++) {
    
                   // if the Product ID number entered by user matches a 
                      Product ID number in the file
                   if (delnum == Product[j].get_id()) {
                      Product[j] = Product[j+1];   // delete record
                      cout << "\nProduct has been deleted. Press any key  
                      to return to main menu.";
                      break;
                   }
                }
                getche();
                system("cls");
                break;
    Can anyone see what I need to change in order to remove the record so that I don't see those strange characters in the spot of the deleted record?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    cross posted and answered there as well.

    DON'T CROSS POST -- it just wastes people's time. If you changed your mind and wanted to create a new thread, you should have deleted your question in the other thread or put a note there saying you were starting a new thread and to post answers to the new thread.
    Last edited by 7stud; 06-15-2005 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM