Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Deleting portion of a file, through C io Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

May 05, 2005, 03:08 PM

Hey everyone!

I just finished (almost) writing my file packer, no compression, no encryption, all is simple now, i can rename, insert, extract files in the archive.

The only thing i can't do, is delete files out of the archive. How would i do that?

It should not matter how my file structure works, its just that i have a set of bytes, and i want to delete some (x length), from a certain point (y offset). I just dont know of a command in C io system that might help me there.

I also do not wish to create new files and overwrite the old ones with the new ones, that's inefficient.
I create a file pointer, such as FILE *p_File; and manipulate from there through fread, fwrite, fseek etc.

Thanks guys!



May 05, 2005, 03:41 PM

I guess you just have to open the file in read-write mode, and read in the portion of the file after what you want to delete and write it in place over the part you want to delete. You can do this in blocks, so you don't need to hold the entire rest of the file in memory at once.


May 05, 2005, 04:38 PM

Thanks for replying, but wouldnt whatever was at the end stay at the end, with the file being longer then it should be?

I mean, if we have a large package, and we want to delete half of it, we would overwrite the first half with the second, but the second one will stay, wouldnt it?



May 05, 2005, 04:45 PM

I would suggest you don't actually shrink the archive every time you remove a file. Instead, just mark that block as empty, and make a seperate purge operation that removes all of the empty blocks at once.


May 05, 2005, 05:03 PM

Could you give me a hint on how i would remove that block???? Thats my actual problem lol.


- Tim

P.S. I think we are getting closer to my problem =)


May 05, 2005, 05:43 PM

Like Reedbeta said you have to rewrite the file without the block.


May 05, 2005, 05:48 PM

But what about my question to Reedbeta?

- TIm


May 05, 2005, 06:32 PM

Right..I forgot about that detail =D

It seems like there should be a way to instruct the C library to write an end-of-file marker, but I can't find how to do it in the MSDN. Failing this, I guess the only way would be to completely rewrite the file.

James Clayton

May 05, 2005, 07:09 PM

Ripped straight out of my most recent demo:

  2. /********************************************************************
  3.  --------------------------------------------------------------------
  4.  -- ftruncate.h
  5.  --
  6.  -- � James Ben Clayton - (2004 / 2005)
  7.  --
  8.  -- Purpose:    Truncates a file to a new size. Only works on Win32
  9.  --------------------------------------------------------------------
  10. *********************************************************************/
  12. #pragma once
  14. #include <io.h>
  16. void ftruncate( FILE * f, long lNewSize )
  17. {
  18.         int iFd = _fileno( f );
  19.         _chsize( iFd, lNewSize );
  20. }


May 05, 2005, 08:32 PM

wow, thanks guys!

That really helps! So im just going to use that little piece of code to "truncate" the file? Thank you, to all of you!


EDIT: "Only works on Win32"?! But what am i supposed to do on linux, mac, and all the other OS's? Is there a more cross-platform way of doing it? Thanks!


May 06, 2005, 02:28 AM

use chsize() on other platforms. It is included in most c-libs, but it's not standard.


May 06, 2005, 05:20 AM

Allright, thanks guys!


This thread contains 12 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.