Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Copy an .exe to a new .exe file using C/C++ (http://www.go4expert.com/forums/copy-exe-exe-file-using-c-cpp-t24818/)

Avenger625 1Feb2011 12:17

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 <stdio.h>
#include <stdlib.h>
#include <conio.h>

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.

And amazingly 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...??!!

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

poornaMoksha 1Feb2011 16:53

Re: Copy an .exe to a new .exe file using C/C++
 
Just see if this helps you

http://www.google.co.in/search?q=has...efd697533c8145

alpha34 1Feb2011 18:24

Re: Copy an .exe to a new .exe file using C/C++
 
borland c++ or any c++ compiler for instance which uses DOS mode to compile and run will not work on windows 7 coz windows 7 now doesn't support DOS properly u need to switch to Dev cpp version for using c or c++ program compilation and running....


All times are GMT +5.5. The time now is 00:05.