Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I had written a source code of C++ and complied it with the same name using the following command line.

For example:

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

Now my source code has been replaced by the executable program.Is there any way to retrieve my source-code. I'm new to Linux so I'm not sure if there is any way to undo what I've done.

share|improve this question

migrated from stackoverflow.com Oct 4 '10 at 7:59

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. –  JoshD Oct 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 Voigt Oct 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. –  paxdiablo Oct 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. –  JoshD Oct 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...) –  DevSolar Oct 4 '10 at 5:17

9 Answers 9

up vote 15 down vote accepted

Probably not, try source control?


You might be lucky enough to have an editor open or a terminal window with scrollback.

And in the locking-the-barn-door-after-the-horse-has-bolted department, a good development practice even when working on toy programs is to use source code control.

Using either git or hg, you can do

$ 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
share|improve this answer

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.

share|improve this answer
    
You can just use: "g++ filename.cpp" and the run with "./a.out". You can disassemble using IDAPro (or some other powerful disassembler) from which you can recover the code. –  jase21 Oct 4 '10 at 6:07
    
@jase21: sure, but probably a.out is not the best filename ever :). IDAPro will not recover the ORIGINAL code anyway. –  nico Oct 4 '10 at 6:10

Which editor did you use. Most probably there might be source-code.cpp~ backup file if you are using Vim or something.

share|improve this answer
    
@jase21 -- I was using Vi editor. thankyou –  Pavitar Oct 4 '10 at 6:01
    
@Pavitar So there definitely will be a backup. Did you find it? –  jase21 Oct 4 '10 at 6:04
    
NO.I guess Vi editor doesn't create a backup. –  Pavitar Oct 4 '10 at 6:15

oops ... you are out of luck.

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!

more here: http://e2undel.sourceforge.net/recovery-howto.html

share|improve this answer

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?

share|improve this answer

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.

My original file test.cpp

#include <iostream>
using namespace std;

int main()
{
        cout << "Hello World!" << endl;
        cout << "Let us C";
}

The last part of my .test.cpp.swp (in this case, it contained a single huge line).

@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
@^@^@^cout << "Let us C";^@   cout << "Hello World!" << endl;^@{^@int main()^@^@using namespace std;^@#i    nclude <iostream>^@

The .swp file get deleted once you close the vi session; so if you've already closed it, you're out of luck.

share|improve this answer

no, unless you can undo your action through Linux, your source code is lost. You probably should have compiled it to a different name.

You cannot reverse-compile your source code.

share|improve this answer
    
Decompilation is possible, but it won't return the original source code. –  Ben Voigt Oct 4 '10 at 5:12
    
It will likely make little sense. –  Alexander Rafferty Oct 4 '10 at 5:22

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.

share|improve this answer

As @jase21 mentioned, if you'd used vim or gedit, there'll be a source.cpp~ file which contains a backup.

emacs will have a #source.cpp# file.

What text editor did you use? On a lighter note, how many lines of code lost?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.