?
Solved

More file stuff

Posted on 2002-04-05
4
Medium Priority
?
221 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
[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
  • 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 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

770 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