[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

delete a record

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.
Thanx..
0
btocakci
Asked:
btocakci
  • 3
2 Solutions
 
PaulCaswellCommented:
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?

Paul
0
 
btocakciAuthor Commented:
They are all in the file,not in memory.
0
 
PaulCaswellCommented:
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.
 
Paul
0
 
PaulCaswellCommented:
And,

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.

Paul
0
 
brettmjohnsonCommented:
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
  fclose(in);

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

  return 0;
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now