Problem with coping an exe file

This is a discussion on Problem with coping an exe file within the C Programming forums, part of the General Programming Boards category; Hi all, i have written some code just to copy an executable but when i try to run the copy, ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Problem with coping an exe file

    Hi all,
    i have written some code just to copy an executable but when i try to run the copy, a get an error(using Windows XP SP 2)
    "Copiedfile.exe is not a valid win32 application"
    and i can't understand why. When i load both files(prototype and copy) at a hex editor they seem to be exactly the same.
    May someone help, i would me most grateful.
    Anyway thanks for your time!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Right there. See? On that line you've made an error. Fix that one, then I'll show you the next one.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Ok man, sorry, i just thought that it had something to do with the OS and not with my code.
    Here:
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(){
        FILE *ptr;
        FILE *ptrout=fopen("fileb.exe","wb");
         long size=0,i;
        if(ptr=fopen("filea.exe","rb"));
        else printf("could not open file");
         fseek (ptr, 0, SEEK_END);
                                   size = ftell (ptr);
                                   
                                   rewind (ptr);
        printf("size=&#37;d\n",size);
        system("pause");
        for(i=0;i<size;i++){
        
       fputc(fgetc(ptr),ptrout);
        printf("i=%d\n",i);}
        fclose(ptr);
        fclose(ptrout);
        
        printf("Successfully Copied!\n");
        
        system("pause");
        return 0;}
    Last edited by tezcatlipooca; 03-30-2007 at 05:58 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    if(ptr=fopen("filea.exe","rb"));
    Many compilers warn you about that and require you to put in extra parentheses
    Code:
    if( ( ptr=fopen("filea.exe","rb") ) );
    but I have no idea why. I'm sure Quzah does though.

    It appears to work well with my (Linux) system. There's no need to determine the filesize beforehand, though.

    I suspect that you're trying to get it to open itself. Most Windows systems don't allow this.

    My only other point is that for UNIX-like systems, you'd probably need to change the file's permissions before executing the new executable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 12:39 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21