Thread: How to remove some bytes at the end of a file?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    How to remove some bytes at the end of a file?

    I need to shorten a big file by removing a couple of bytes at the end of a file.

    Is this possible without (temporary) copying to another file?

    In other words, I want to change the filesize (make it shorter).

    Greetz.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    I am not sure I have the answer for you. But you will get better advice if you explain "how" you know which bytes are extra. Is it just you want to remove two bytes no matter what they are? Or do you want to remove bytes between the last EndOfLine (\n) and the end of the file? Or ...

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Quote Originally Posted by pheininger View Post
    I am not sure I have the answer for you. But you will get better advice if you explain "how" you know which bytes are extra. Is it just you want to remove two bytes no matter what they are? Or do you want to remove bytes between the last EndOfLine (\n) and the end of the file? Or ...
    I want to remove the last n bytes from a file, no matter what the content is.

    In the mean time, I found this: File Size - The GNU C Library

    Problem is, truncate() is not portable. (I use GCC on Linux and MinGW on windows)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Sticking to standard C, the only way to accomplish this is to do what you said, write a copy of the file excluding the excess bytes, remove the original and rename the file.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Not everything can be done in "standard C". Almost all real programs have nonportable parts. The key is to recognize and document them as such, and keep them separate from the portable parts. Then implement them for the systems you're targetting and selectively include the proper one for each system using the preprocessor. Here's the c-faq's (partial) answer to file truncation: Question 19.13
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file i/o and remove something
    By bnkslo in forum C Programming
    Replies: 22
    Last Post: 06-24-2008, 01:38 PM
  2. remove a file
    By behzad_shabani in forum C Programming
    Replies: 6
    Last Post: 06-17-2008, 09:07 AM
  3. Hex Editing - Remove first 15 bytes
    By xTrinity in forum C++ Programming
    Replies: 13
    Last Post: 08-13-2003, 09:01 PM
  4. Remove File
    By krakz in forum C Programming
    Replies: 8
    Last Post: 12-22-2002, 03:54 PM
  5. how to know how much bytes does this file contain?
    By mickey in forum C++ Programming
    Replies: 5
    Last Post: 10-09-2002, 02:50 AM