Thread: EOF/File size reduction

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    EOF/File size reduction

    Hi,

    I am looking to remove certain sections (ranges of bytes) from files. I used fseek/fwrite/fread to essentially "compact" the data by overwriting the data I was removing with what came after it. After this procedure, there is data on the end of the file which doesn't belong. I need to somehow "truncate" my file at a certain point. I heard you could write EOF to the file and the OS would handle discarding the stuff after, thus giving the desired effect. I actually need the file to shrink by the amount of bytes I desired to remove. However, I haven't found any way to do this with the C or C++ libraries. Anyone able to help me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Normal file reading and writing cannot make a file shorter (except by starting a new file and just copying the bits you need)

    Various non-standard functions exist which can do what you want, for example
    http://man.he.net/?topic=truncate&section=2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Maybe I get you wrong, but in case your intention is saving storage space, you should also consider clustering.
    Depending on the OS - more precisely file system type, the space *really* needed by a file is rounded up to the smallest allocation unit provided by the given file system. Therefore you might be struggling to save a few bytes, but there's you end up saving nothing...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM