Copy an .exe to a new .exe file using C/C++

Let there be an executable file say, t1.exe. If I run t1.exe then it shall copy itself to a new executable file say t2.exe.

Code:

#include
#include
#include

void main()
{
FILE *fp,*tp;
unsigned long int t=0;
tp=fopen("t2.exe","wb");
//rewind(tp);
if((fp=fopen("t1.exe","rb"))!=NULL)
{
fseek(fp,0L,2);
unsigned long int pos=ftell(fp);
rewind(fp);
do{
fputc(fgetc(fp),tp);
t++;
}while(t!=pos+1);
}

fcloseall();
//system("t2.exe");
printf("End of t1");
getch();
}

t2.exe is created but when I run this it says "t2 has stopped working." I'm using Windows 7(Home Basic) 64bit. The program is compiled with Borland C++ V. 5.02.


Please reply fast!!! It's URGENT....!!!!
Thnx!




Comments

  • I've never tried file i/o like this, but what you might try is something like.

    [code]
    int ch;

    if(fp != NULL && tp != NULL) {
    while((ch = fgetc(fp)) != EOF) {
    fputc(ch, tp);
    }

    fclose(fp);
    fclose(tp);

    // add a slight delay here
    system("t2.exe");
    }[/code]



  • Hello,

    I don't have code for you, but I want to ask you something. Such a techique as you want to use is most often used for viruses or something like that, things which have to spread unknown.

  • #include
    #include
    #include
    #include

    void main()
    {
    FILE *fp,*tp;
    int ch;
    tp=fopen("t2.exe","wb");
    fp=fopen("t1.exe","rb");
    if(fp != NULL && tp != NULL) {
    while((ch = fgetc(fp)) != EOF) {
    fputc(ch, tp);
    }

    fclose(fp);
    fclose(tp);
    sleep(5);
    // add a slight delay here
    system("t2.exe");
    }
    getch();
    }


    As per your outlined code I tried sumthing like the above but it also has the same problem as my code. And amazingly (in both cases) t1.exe and t2.exe has exactly same file size(in bytes) but still t1.exe works but t2.exe doesn't......don't understand why...??!!


    As far as virius and stuffs are concerned I really don't have any such destructive ideas.....!!!!

    Please help.....this is really URGENT!!! I would request the senior members to deal with this issue,please......

    Thnx!

  • make filename "t"+(char)a+".exe"
    [b]not[/b] t1 nor t2
    just use a variable for filename bro!
    not constants
    because it must increase if ye want a copy-cat
  • dont try at home
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories