Solved

More file stuff

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

731 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