Click to See Complete Forum and Search --> : Copying File Manualy


Quell
June 16th, 2005, 05:10 AM
Hello.
I tried to make a function that will make a copy of a file:

sFile sfFile;
sfFile.chFile=new CH[strlen(chFile)*sizeof(CH)];
memcpy(sfFile.chFile,chFile,strlen(chFile)*sizeof(CH));
sfFile.hFile=CreateFile(chFile,FILE_ALL_ACCESS,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(sfFile.hFile == INVALID_HANDLE_VALUE)Log("Invalid Handle on CreateFile");
sfFile.dwSize=GetFileSize(sfFile.hFile,NULL);
sfFile.uchData=new unsigned char[sfFile.dwSize];
if(ReadFile(sfFile.hFile,sfFile.uchData,sfFile.dwSize,&sfFile.dwSizeRead,0)==0)Log("Erro on Reading FIle");
FILE *store_point;
store_point=freopen("copy.exe","w",stdout);
for(int i=0;i<sfFile.dwSize;i++){
cout<<sfFile.uchData[i];
}

Where the structure is:

typedef struct sFile{
unsigned char *uchData;
CH *chFile;
HD hFile;
DW dwSize;
DW dwSizeRead;
};

Ch=char

Basicly teh output i get is close to the right one, it is 1 kb over the size but the file is no longer executable, if i try to copy an executable file.
I don't want to use winapi for copying files......
Anyways, any ideas where i made a mistake?
Thx alot in advance

golanshahar
June 16th, 2005, 05:25 AM
i understand u dont want to use winpai to copy the file, but what about

system("copy filename1 filename2").


?

if i helped dont forget to rate :-)
Cheers

golanshahar
June 16th, 2005, 05:29 AM
btw if you using ::CreateFile(..) why not using ::CopyFile(..)??


Cheers

Quell
June 16th, 2005, 05:54 AM
i just wanan make some changes to teh file->compress it, but i need to be able to output teh file.
so any ideas where i made a mistake.
and thx for your answers and alt solutions :D

Hobson
June 16th, 2005, 06:04 AM
If text files seem to be copied OK, and binary files fail to get copied, make sure that you open your new file in binary mode, and not text mode. If you are using defaults, check what they are.

Hob

Philip Nicoletti
June 16th, 2005, 06:05 AM
I'm not familiar with any of the windows API file handling routines,
but typically the problem you describe occurs when the output
file is not opened in binary mode.

Quell
June 16th, 2005, 12:42 PM
thx alot i asked the question in winapi section about the mode, the msdn does not specify any of this info.
:D