CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    Copying File Manualy

    Hello.
    I tried to make a function that will make a copy of a file:
    Code:
        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:
    Code:
    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

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Copying File Manualy

    i understand u dont want to use winpai to copy the file, but what about
    Code:
    system("copy filename1 filename2").
    ?

    if i helped dont forget to rate :-)
    Cheers

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: Copying File Manualy

    btw if you using ::CreateFile(..) why not using ::CopyFile(..)??


    Cheers

  4. #4
    Join Date
    Aug 2003
    Posts
    938

    Re: Copying File Manualy

    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

  5. #5
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Copying File Manualy

    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
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Copying File Manualy

    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.

  7. #7
    Join Date
    Aug 2003
    Posts
    938

    Re: Copying File Manualy

    thx alot i asked the question in winapi section about the mode, the msdn does not specify any of this info.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured