Solved

More file stuff

Posted on 2002-04-05
4
187 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

21 Experts available now in Live!

Get 1:1 Help Now