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.
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.
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)
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); //}
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