gpt4 book ai didi

c - WWindow 创建文件 读取文件 写入文件

转载 作者:行者123 更新时间:2023-11-30 21:37:41 24 4
gpt4 key购买 nike

我是一名学生,因此我为没有使用正确的论坛协议(protocol)而预先道歉。我是 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;

}

最佳答案

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

#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;
}

关于c - WWindow 创建文件 读取文件 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com