This question came from our site for professional and enthusiast programmers.

21

Oh Myyy.. This is a great example of why we use source control.
–
JoshDOct 4 '10 at 5:07

2

Voting to move to super-user, since this is OT for SO. I do feel for ya Pavitar.
–
Ben VoigtOct 4 '10 at 5:10

@JoshD: Source control may not have helped in the situation where you check out, make a hundred changes, then compile to test before checking in (rule number 1: never break the build). What I long for are the days of VMS, where every file was versioned whenever it changed, and you could just back up one version to recover the source.
–
paxdiabloOct 4 '10 at 5:12

@paxdiablo: very true. In that case, use a text editor that automatically makes backup in .bak files on each save :) Journaling file systems seem to be coming into style, which kinda do what you've said. I think apple computers have just that capability.
–
JoshDOct 4 '10 at 5:14

4

@paxdiablo: That is why you have development branches, which you merge back once your changes got stable. (Ahem. That's also why you have Makefiles with tested-good compiler statements...)
–
DevSolarOct 4 '10 at 5:17

$ hg init
$ hg add source.cpp
$ hg commit -m 'change' source.cpp
$ # edit here, and you can optionally revert to the original
$ hg commit -m 'change' source.cpp
$ # now if you clobber it you can go back to one of the previous revisions

The option -o specifies the output file, that is why the source code was overwritten.

You should have used

c++ source-code.cpp -o executable-name

As for retrieving the original source from the compiled file: no you cannot. You could disassemble it (so get an assembly version of your program) and I'm sure there is some little program out there that will rewrite some "C++ style" code from it, but that will never be like your original code as more than one instruction in C++ may correspond to the same machine code.

First of all: Immediately unmount the
file system the deleted file was
located on. This minimizes the risk
that the data of the deleted file are
overwritten while taking steps to
recover them. All data written to
the file system containing the deleted
file - either by you or by any other
process running on your machine -
might overwrite some of the data you
want to recover!

Try some disk recovering tool maybe the new file wasn't written on the same blocks as the original one. I am just thinking out loud here, but its better that you gave this a shot. Btw which editor are you using? Have you checked if your editor creates an automatic backup of every file?

In the highly unlikely event that you haven't closed your vi session yet, open a new terminal and look for a .source-code.cpp.swp file in the same folder. Remember that files starting with a period are not listed by ls command by default; use ls -A to see them.

The last line of the .swp file would have the text from your original file before being corrupted by mal-compilation.

I won't say it's impossible. The FBI can probably get some portion of it back with a huge heaping helping of luck. But since your executable is certainly larger than your source, you've overwritten all of it.

This is one reason why you should use a version control system.
Check out SVN.