-1

Sadlly, I compiled a .c file with the following command:

$ gcc a.c -o a.c

so a.c became the executable file covering my old c file... I want to ask can i have the real a.c file back and how ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
coperd
  • 9
  • 4

4 Answers4

0

Unfortunately, that's close to impossible, unless you're adept at reverse-engineering binary code, AFAIK.

amrith92
  • 220
  • 4
  • 15
0

You can restore it from a backup, or you can restore it from your source code control system.

If you have neither of those, you should start using both (and now you know why!).

You may also be able to recover the file with file recovery tools for the OS you are using, or failing that, you may be able to decompile the object file. Here is a decompiler:

http://boomerang.sourceforge.net/cando.php?hidemenu

That was the first one on the list after searching Google with "c decompiler".

Here's a stackoverflow question about decompilers.

What's a good C decompiler?

Community
  • 1
  • 1
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Decompiling an executable into C source code is like unscrambling an egg. You will *not* get back your original source code; the compiler and linker throw away too much information, including comments, layout, type information, and most identifiers. The best you can expect is a very different C source file, probably unmaintainable, that compiles to the same executable. Don't expect the decompiled (or disassembled) source to be anything more than a guideline for re-writing your program from scratch. – Keith Thompson Mar 31 '13 at 19:22
0

When you overwrite a file, the operating system does not physically put the data exactly in the same spot, it simply marks the old data as available for use, and writes the new data to the disk. It might overwrite your file, it might not. Midnight commander has a tool that can try to get your file back.

Currently the Midnight Commander is packaged with some Virtual File Systems (VFS): ... the undelfs, used to recover deleted files on ext2 file systems (the default file system for Linux systems)

Midnight commander guide

In the mean time, you should probably not save any other files on the computer, especially in the directory your deleted file is in.

EDIT: By the way, this is an old article, linux hasn't used ext2 for a while. But I think it should still work.

iCodeSometime
  • 1,444
  • 15
  • 30
0

grep is a great tool to recover lost C files.

grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > recover.txt

The above command recovers the lost text 25 lines before and 100 lines after the 'some string in the file' line in your C file and outputs the recovered text in in recover.txt file. So you can change the value of 25 and 100 as desired. The above command also assumes the partition name is /dev/sda1. You can use the mount command to find your partition name.