0

I would like to be able to edit the contents of a binary file in C++, and remove all the contents up to a certain character position, which I already know, a bit like removing a header from a file.

One example file I have contains 1.3 million bytes, and I would like to remove the first 38,400, then save the file under the original name.

Currently, I've been doing a buffered read to find the position for each file (the rules for where to cut the file are complex and it is not a simple search), and of course, I could do another buffered read from pos, outputting into a new file, then do a rename, or something along those lines.

But it feels quite heavy handed to have to copy the entire file. Is there any way I can just get the OS (Windows Vista & upwards only - cross-platform not required) to relocate the start of the file, and recycle those 38,400 bytes? Alas, I can find no way, hence why I would ask of you your assistance :)

Thank you so much for any help you can offer.

niemiro
  • 1,778
  • 1
  • 20
  • 37
  • Is copying the entire file taking too long? – willj Aug 05 '13 at 16:18
  • It isn't taking any longer than I had expected, but some of the files are over 2GB, so aren't tiny either. Anyway, thank you all so much for your help. Greatly appreciated :) – niemiro Aug 05 '13 at 16:31
  • I've just found some further explanation as to why it's not implemented: http://blogs.msdn.com/b/oldnewthing/archive/2010/12/01/10097859.aspx – niemiro Aug 05 '13 at 16:39
  • New link for the above article: https://devblogs.microsoft.com/oldnewthing/20101201-00/?p=12153 – niemiro Jun 03 '20 at 17:32

3 Answers3

2

No, there is no support for removing the head of the file in any OS I'm familiar with, including any version of Windows. You have to physically move the bytes you want to keep, so they end up at the start. You could do it in place with some effort, but the easiest way is as you suggest - write a new file then do rename-and-delete dance.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
1

What you're looking for is what I call a "lop" operation, which is kind of like a truncate, but at the front of the file. I posted a question about it some time back. Unfortunately, there is no such thing available.

Community
  • 1
  • 1
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
1

Simply overwrite file (by fixed memory blocks) from neccessary pos to the begin of file.

const size_t cBufSize = 10000000; // Adjust as you wish
const UINT_PTR ofs = 38400;
UINT_PTR readPos = ofs;
UINT_PTR writePos = 0;
UINT_PTR readedBytes = 0;
1. BYTE *buf = // Allocate cBufSize memory buffer
2. Seek to readPos
3. Read cBufSize bytes to buf, remember actual readed number of bytes to readedBytes 
4. Seek to writePos
5. Write readedBytes from buf
6. Increase readPos and writePos on readedBytes value
7. Repeat from step 2, until you get end of file.
8. Reduce file size on ofs bytes.
Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61