delete a record

Posted on 2006-06-03
Last Modified: 2010-04-15
Hi! i want to delete a record from a stucture array,with this, all the next structures will shift by one so that the array will shrink..
The way i know to write all the files into another one. Is there any better way to this without openin' a new file and rename it as the old one. I dont want any codings, just an opinion.
Question by:btocakci
    LVL 16

    Expert Comment

    Hi btocakci,

    For removing records from files, there really isnt any way of doing this without creating a new file. If the array is in memory its easier. Which is it?


    Author Comment

    They are all in the file,not in memory.
    LVL 16

    Accepted Solution

    Hi btocakci,

    There's essentially four ways of doing this:

    1) Make a complete copy of the original file except for the record(s) you want to remove.

    Upside: This is the easiest to code and can often be significantly faster than '2)' below.
    Downside: If the file is large, this can take use up a large amount of disk space during the process.

    2) Copy the data 'in situ'. Walk a read and a write pointer up the file. Start the write pointer at the start of the record to be deleted and start the read pointer at the end of it.

    Upside: Little storage overhead.
    Downside: Usually much slower.

    3) Hybrid between the above two.

    Store your data in many small files.

    Upside: You can perform method 1 above for a much smaller file.
    Downside: Can be lots of files.

    4) Dont delete the records, just mark them as empty.

    Add a 'deleted' flag to every record.

    Upside: Very quick to delete records.
    Downside: Extra code to check the flag can get difficult to manage. Data files need compacting occasionally.
    LVL 16

    Expert Comment


    5) Use an index file.

    Use a separate file that contains offsets into the main file where each record starts.

    Upside: As 4) above plus sorting is quicker as the index is sorted, not the data.
    Downside: Increased disk hit for each record read. Compaction also required.

    LVL 23

    Assisted Solution

    Here is some sample code that does Paul's suggestion #2:
    [I just had this lying around.]

    /* dellines.c
     * This removes all lines beginning with '#'
     * from the supplied file.  It does this in-place
     * [rather than safely writing to a temp file and
     * replacing the original].

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main(int argc, char** argv)
      FILE * in, *out;              // input and output file descriptors
      char buffer[32768];   // i/o buffer

      // open the input file
      in = fopen(argv[1], "r");
      if (in == NULL) {
        perror("Unable to open input file");
        return -1;

      // Reopen the same file for output and position it at
      // beginning of file for writing.
      out = fopen(argv[1], "r+");
      if (out == NULL) {
        perror("Unable to open output file");
        return -1;
      //fseeko(out, 0, SEEK_SET);
      // read each line of the file, write out those lines that
      // don't begin with '#'.  Since we only delete (rather than
      // insert) lines, we know the output will never catch up to
      // the input.
      while (fgets(buffer, sizeof(buffer), in)) {
        if (buffer[0] != '#')
          fputs(buffer, out);
      // close the input

      // truncate the output to the new length
      ftruncate(fileno(out), ftello(out));

      return 0;

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now