Thread: Accidentally Compiled a .c file over itself....

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Accidentally Compiled a .c file over itself....

    I have a problem, when using the gcc -Wall some.c -o some.c command I wasnīt paying attention and wrote over the original c source file with itīs output program. So now some.c is the actual program and the source file no longer exists. Is there anyway to back track so I donīt have to re-write the entire source file? Maybe in visual studio, I have that installed on my computer? I have no idea if I can actually ĻdecompileĻ code or not...Iīm a newb. Iīm in a jam and I need an answer by tonight lol, Iīm so ........ed off!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unfortunately, you can't "decompile" code - it's a "one-way process". You may be able to find the source as a backup file if you have configured your editor to create backup files, but other than that, it's a bit of "tough luck" situation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    really? damn

    That sucks, I mean it only has a main function, nothing fancy just a long list of control structures so it´s going to be a pain. I guess it will teach me to be more careful in the future. So there´s really no way to reverse engineer a basic compiled c source file? I would of assumed since I´m not working on anything High level it would be easy. Unfortunately i´m writing c source files on my linux platform so unless I specify backups it won´t make them. So now I know, create backups...lol.

    p.s. There´s no way to restore the file to it´s original state either, since it was overwritten I mean...?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are using Emacs to edit, it should make a xxx.c~ file for you every time you save the file, which contains the file as it was before you started changing it.

    And the correct style for "a bunch of structures" is to stick those in a header-file, generally speaking. So if you have to redo it anyways, try to use a separate header file for your data structures - that way they are usable in other pieces of code too, as well as being safe from being "overcompiled". [One would of course sort of think that gcc would "recognise that the output file and input file are the same name and refuse to compile, but I guess there are valid cases for that in some circumstances, e.g. gcc -E somefile.c -o somefile.c would work ok in the right circumstances, so now we'd have to introduce an extra switch to say "yes, I know, the input and output are the same, but I'm ok with that].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Everyone does something like that at some point; It teaches you to make sure it never happens again.
    Just be thankful that you were able to learn this lesson from only losing such a small amount of code, unlike some of us.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want inspiration, ask @nthony, who did not make such a mistake and yet ended up in a situation like yours.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This was discussed recently.
    http://cboard.cprogramming.com/showthread.php?t=93056

    Be warned, this is hard work, with mixed levels of success, and diminishing returns the longer you wait after the event.

    Gah, well beaten!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    I ran into some luck

    I was lucky to find an old version of the same c source file. Still needs alot of tweaking but it's better than nothing. I work on a dual boot system so sometimes I have cluttered files when i'm working on a project. Looks like I got a long night ahead of me though . Since I have you guys here, when writing to a file. Can I continously write blank lines, spaces, and words to the same file after it's been opened. Or can I only write one piece of information, then append it everytime? I'm using fopen(out.txt, "w")...then simply fprintf to write blank lines, spaces, or words to that file. Is this the correct process? Or what i'm trying to ask I guess, is proper ways to format when writing to file, should I be appending to create a constant flow of strings, or does writing to file write on the same line unless specified otherwise?
    Last edited by John_L; 09-23-2007 at 02:09 PM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can move the "file-pointer" within a file, using fseek(). There is a problem with text-files, and that is that you can't overwrite a piece of text with a shorter or longer piece without "shuffling" all the remaining pieces of the file - in this case you'd need to read the whole (rest of) the file into memory, and writing it back out again after the new text. If all your lines are exactly the same length, then it's easy, but if you have lines that vary in length, then the new line will have to be written with extra spaces on the end if it's shorter, and a longer line will write over the beginning of the next line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM