Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to delete one record in a structure?

Posted on 1999-07-03
7
Medium Priority
?
418 Views
Last Modified: 2008-02-20
can somebody tellme how to delete a one record in a file?
0
Comment
Question by:eddyhalim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Accepted Solution

by:
FuzzyLogic earned 0 total points
ID: 1263182
Easiest way is to copy all the other records to another file, and then erase the old one and rename the new one.

0
 
LVL 84

Expert Comment

by:ozo
ID: 1263183
Do you care about preserving the order of the other records?
0
 

Author Comment

by:eddyhalim
ID: 1263184
please give me the step and the source for example.
0
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.

 
LVL 2

Expert Comment

by:hougaard
ID: 1263185
Another solution would be to have a member of the structure defining the state of the record, so that you can mark it deleted ... Like the old dBase II files... A some point you can then use FuzzyLogic's suggestion to compact the file.
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1263186
... Like the WinWord .DOC files... (Got bless Macro$oft ;)
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1263187
#include <stdio.h>
#include <stdlib.h>


typedef struct {
    long l;
    char s[20];
} Unit;         // Your structure


void main() {
    FILE *fin;
    FILE *temp;
    Unit u;

    const char filename[] = "Whatever.dat";

    fin = fopen(filename, "r");
    temp = tmpfile();

    if (fin==NULL || temp==NULL) {
        exit(-1); // Something wrong.
    }

    while (fread((void *)&u, sizeof(Unit), 1, fin)>0)
        if (1)  // Your test here
            fwrite((void *)&u, sizeof(Unit), 1, temp);

    fclose(fin);

    fin = fopen(filename, "w");
    rewind(temp);

    while (fread((void *)&u, sizeof(Unit), 1, temp)>0)
        fwrite((void *)&u, sizeof(Unit), 1, fin);

    fclose(fin);
    fclose(temp);  // tempfile is erased when closed.

}
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1263188
eddyhalim - you must have cought me in a good day... Usually I don't write code for 5pt.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

688 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