0

I am a beginner C++ programmer.

I want to create a binary file, that is uncleared with the previous information that was in it. This is easy to do with RAM, simply by making an array, but how do I do this on a hard drive?

How do I create a uncleared file?

In other words how do I retrieve data that was not "cleared" but just marked "empty".

However, if the OS does not allow it, can I launch linux from USB and run my software?

user2990508
  • 255
  • 1
  • 4
  • 11
  • 3
    "I want to create a binary file" - would be in direct contradiction with "with the previous information that was in it.". The latter implies it has already been created, somehow. Are you asking how to open a file, and if so, have you done any research on C++ file operations *at all* ? – WhozCraig Jun 05 '15 at 17:01
  • 6
    @WhozCraig I assume OP wants to create a (say) 1MB file and inherit the contents of whatever deleted file used to be stored at that location on the disk. The answer is that most operating systems disallow this, for security reasons. – Raymond Chen Jun 05 '15 at 17:03
  • @RaymondChen Yeah, your operating system will move mountains to ensure that the file is zeroed out before you get it, though unless you're using sparse files this is never an issue. In order to get data out, you **must** write data in. – tadman Jun 05 '15 at 17:04
  • @RaymondChen that you pulled that from that question is *awesome* (and probably correct). Love your website, btw. – WhozCraig Jun 05 '15 at 17:05
  • You can create a file and write some garbage into it from your "garbage" memory blocks. If you really want garbage from disk, you can read some using "sector reading" and dump to your own file. – Tim3880 Jun 05 '15 at 17:10
  • "if the OS does not allow it, can I launch linux from USB and run my software?" The two questions seem unrelated. This is like "If I can't pay with a credit card, can I have a puppy?" – Raymond Chen Jun 05 '15 at 17:16
  • possible duplicate of [Deleted file recovery program using C C++](http://stackoverflow.com/questions/3813024/deleted-file-recovery-program-using-c-c) – NathanOliver Jun 05 '15 at 17:16
  • 1
    @RaymondChen I'm guessing the use case is that he's trying to snoop on a computer and if he can't do it from windows, he wants to put linux on a thumbdrive, boot into that environment, and snoop from there. So it is kind of related... kind of. – RyanP Jun 05 '15 at 17:23

1 Answers1

0

To keep the content of a file to be written on, you can open a file in append mode with:

[ofstream ofs ("filename", ios::binary | ios::app);][1]      

All output operations append at the end of the file. Alternatively, you could also use ios::ate so that the output position starts at the end of the file (but afterwards it's up to you).

With the usual read operations you can retrieve preexisting content, by first positionning yourself using seekp().

Christophe
  • 68,716
  • 7
  • 72
  • 138