How can I get the length of the contents of a HANLDE?

1

I need to do a program in to copy the content of a file in another file. I'm using HANDLE objects to open the source and destination file and I'm using DWORD to read the content. The code is as follows

HANDLE hIn = CreateFile("testSource.txt", FILE_READ_DATA, 0 , NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hOut = CreateFile("testTarget.txt", FILE_WRITE_DATA, 0 , NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
char buf[30] = {'
HANDLE hIn = CreateFile("testSource.txt", FILE_READ_DATA, 0 , NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hOut = CreateFile("testTarget.txt", FILE_WRITE_DATA, 0 , NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
char buf[30] = {'%pre%'};

if(hIn == INVALID_HANDLE_VALUE || hOut == INVALID_HANDLE_VALUE)
{
    printf("No existe alguno de los archivos\n");
} else
{
    DWORD readText;
    ReadFile(hIn, buf, 36, &readText, NULL);
    WriteFile(hOut, buf, 36, &readText, NULL);
}

CloseHandle(hIn);
CloseHandle(hOut);
'}; if(hIn == INVALID_HANDLE_VALUE || hOut == INVALID_HANDLE_VALUE) { printf("No existe alguno de los archivos\n"); } else { DWORD readText; ReadFile(hIn, buf, 36, &readText, NULL); WriteFile(hOut, buf, 36, &readText, NULL); } CloseHandle(hIn); CloseHandle(hOut);

The problem is that the functions ReadFile and WriteFile need to have the size that you want to read or that you want to write. How can I obtain the number of characters contained in the HANDLE object of the file I wrote so that I can indicate how many characters I am going to write in the destination file?

    
asked by Dani Garcia 29.08.2016 в 05:06
source

1 answer

1

The GetFileSizeEx function allows you to obtain the size of a file, in bytes, passing it the HANDLE .

But if all you want is to copy the contents of a file, using the Windows API, you might want to call CopyFile directly.

    
answered by 29.08.2016 в 06:23