Solved

More file stuff

Posted on 2002-04-05
4
179 Views
Last Modified: 2010-07-27
After using fseek() to locate parts of files, how can I get rid of them? (Directly from file)  Is there another function I can use?
Thank You!
0
Comment
Question by:pingle
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6921399
You mean, you wat to delete a section from a file? That's not possible. All you could do is "copying" the file while going through it and skipping the part you don't want...
0
 

Author Comment

by:pingle
ID: 6921434
Could you tell me how to do that?  That should serve my purpose just fine.
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6921496
Well, if you had e.g. 6 records of the same size in a file and want to skip the 4th, you'd

#include <stdio.h>

struct RECORD {

 int i1;
 int i2;
 int i3;
 int i4;
};
void main () {

RECORD record;

 FILE* pIn = fopen ( "records.fil", "rb");
 FILE* pOut = fopen ( "new_records.fil", "wb");

 for ( int i = 0; i < 6; i++) {

   fread ( &record, sizeof ( RECORD), 1, pIn);

   if ( 3 == i) continue; // skip the 4th record at index 3

   fwrite ( &record, sizeof ( RECORD), 1, pOut);

 }

 fclose ( pIn);
 fclose ( pOut);
}

(error checking omitted for brevity)
0
 

Author Comment

by:pingle
ID: 6921540
Thank you AGAIN! I really appreciate it!
~Pingle
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

10 Experts available now in Live!

Get 1:1 Help Now