使用CreateFile ReadFile WriteFile

时间:2015-02-07 18:55:53

标签: c api winapi createfile

我是学生,所以我为没有使用正确的论坛协议而道歉。我是C新手,真的是Win32 API的新手。我的任务是写一个小的C程序,只使用Win32 I / O系统调用将现有文件的内容复制到一个新文件:CreateFile(),ReadFile(),WriteFile()等...文件名是在命令行上指定。现在我只是尝试了解基本功能,然后我将专注于错误处理。此代码编译,创建一个新文件,但数据不会被复制到它。有什么建议?谢谢你看看!

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

int main(int argc, char *argv[])
{
char buff[4096];
DWORD dwBytesRead, dwBytesWritten;
DWORD dwBytesToWrite = (DWORD)strlen(buff);

//open source file and read it
HANDLE source;

// Create a handle for the source file
source=CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

// Check for errors
if ( source == INVALID_HANDLE_VALUE ){
    printf("Error, source file not opened.");
    exit(EXIT_FAILURE);
}
else printf("The source file is %s\n", argv[1]);

//create a new file
HANDLE target;
target = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if ( target == INVALID_HANDLE_VALUE ){
    printf("Error, target file not created.");
    exit(EXIT_FAILURE);
}
else printf("The source file is %s\n", argv[2]);

//copy contents
ReadFile(source, buff, 4096, &dwBytesRead, NULL);
WriteFile(target, buff, dwBytesToWrite, &dwBytesWritten, NULL);

//copy complete
CloseHandle(source);
CloseHandle(target);

return 0;

}

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,您的代码中存在一些错误。尝试更像这样的东西:

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

int main(int argc, char *argv[])
{
    char buff[4096];
    DWORD dwBytesRead, dwBytesWritten;

    // Open the source file
    HANDLE source = CreateFileA(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    // Check for error
    if (source == INVALID_HANDLE_VALUE) {
        printf("Source file not opened. Error %u", GetLastError());
        return EXIT_FAILURE;
    }
    printf("The source file is %s\n", argv[1]);

    // Create a new file
    HANDLE target = CreateFileA(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    // Check for error
    if (target == INVALID_HANDLE_VALUE) {
        printf("Target file not created. Error %u", GetLastError());
        CloseHandle(source);
        return EXIT_FAILURE;
    }
    printf("The target file is %s\n", argv[2]);

    // Copy contents
    bool ok = true;
    do {
        // Read file, check for error
        if (!ReadFile(source, buff, sizeof(buff), &dwBytesRead, NULL)) {
            printf("Source file not read from. Error %u", GetLastError());
            ok = false;
            break;
        }

        // Check for EOF reached
        if (dwBytesRead == 0) {
            break;
        }

        // Write file, check for error
        if (!WriteFile(target, buff, dwBytesRead, &dwBytesWritten, NULL)) {
            printf("Target file not written to. Error %u", GetLastError());
            ok = false;
            break;
        }
    }
    while (true);

    // Copy complete

    CloseHandle(source);
    CloseHandle(target);

    // Check for error
    if (!ok) {
        DeleteFileA(argv[2]);
        return EXIT_FAILURE;
    }

    // all OK
    return 0;
}