Из plain C в C++ Win32 API - копирование файлов

Рейтинг: -2Ответов: 1Опубликовано: 27.02.2015

Есть код на C:

#include <stdio.h>
#include <stdlib.h>

#define COPY_BLOCK_SIZE 64
#define PROGRAM_NAME argv[0]
#define INPUT_FILE_NAME argv[1]
#define OUTPUT_FILE_NAME argv[2]

int main(int argc, char ** argv) {
unsigned char buf[COPY_BLOCK_SIZE];
FILE * fin, * fout;
size_t bytes;

if ( argc < 3 ) {
    fprintf(stderr, "Usage: %s input_file output_file\n", PROGRAM_NAME);
    getchar();
    exit(1);
}

if ( ! ( fin = fopen(INPUT_FILE_NAME, "rb") ) ) {
    fprintf(stderr, "%s: can't open %s for input!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    getchar();
    exit(1);
}
if ( ! ( fout = fopen(OUTPUT_FILE_NAME, "wb") ) ) {
    fprintf(stderr, "%s: can't open %s for output!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
    if ( fclose(fin) )
        fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    getchar();
    exit(1);
}

while ( ( bytes = fread(buf, sizeof(char), COPY_BLOCK_SIZE, fin) ) != (size_t)EOF && bytes > 0 ) {
    if ( fwrite(buf, sizeof(char), bytes, fout) != bytes ) {
        fprintf(stderr, "%s: can't write to %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
        if ( fclose(fin) )
            fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
        if ( fclose(fout) )
            fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
        getchar();
        exit(1);
    }
}

if ( ferror(fin) ) {
    fprintf(stderr, "%s: can't readf rom %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    if ( fclose(fin) )
        fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    if ( fclose(fout) )
        fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
    getchar();
    exit(1);
}

if ( fclose(fin) ) {
    fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    if ( fclose(fout) )
        fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
    getchar();
    exit(1);
}

if ( fclose(fout) ) {
    fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
    getchar();
    exit(1);
}

fprintf(stderr, "Done.\n");
getchar();
exit(0);
}

Помогите, пожалуйста, переделать, чтобы использовались возможности Win32 API. То есть чтобы переменные были WINAPI-шных типов (DWORD, CHAR и т. д.), использовались аналоги функций из windows.h типа CreateFile, WriteFile (Только не CopyFile :) надо вручную). А то сам в винапи ни бум-бум, а надо срочно. Спасибо.

Ответы

▲ 1Принят

Короче говоря, вот, поковырял сам:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define COPY_BLOCK_SIZE 64
#define PROGRAM_NAME argv[0]
#define INPUT_FILE_NAME argv[1]
#define OUTPUT_FILE_NAME argv[2]

int main(int argc, LPTSTR argv []) {
CHAR buf[COPY_BLOCK_SIZE];
HANDLE hin, hout;
DWORD bytes;

if ( argc < 3 ) {
    fprintf(stderr, "Usage: %s input_file output_file\n", PROGRAM_NAME);
    getchar();
    exit(1);
}

if ( ( hin = CreateFile (argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL ))
             == INVALID_HANDLE_VALUE ) {
    fprintf(stderr, "%s: can't open %s for input!\n Error: %x\n", PROGRAM_NAME, INPUT_FILE_NAME, GetLastError());
    getchar();
    exit(1);
}

if ( ( hout = CreateFile (argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ))
             == INVALID_HANDLE_VALUE ) {
    fprintf(stderr, "%s: can't open %s for output!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
    if ( ! CloseHandle(hin) )
        fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
    getchar();
    exit(1);
}

while ( ReadFile(hin, buf, COPY_BLOCK_SIZE, &bytes, NULL ) && bytes > 0 ) {
    if ( ! WriteFile ( hout, buf, bytes, &bytes, NULL )) {
        fprintf(stderr, "%s: can't write to %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
        if ( ! CloseHandle(hin) )
            fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, INPUT_FILE_NAME);
        if ( ! CloseHandle(hout) )
            fprintf(stderr, "%s: can't properly close %s!\n", PROGRAM_NAME, OUTPUT_FILE_NAME);
        getchar();
        exit(1);
    }
}

fprintf(stderr, "Done.\n");
getchar();
exit(0);
}