欢迎关注我的新博客 https://pino-hd.github.io,最新的博文都会发布在上面哦~
Windows编程之文件操作
该程序主要功能就是创建一个文件,然后在文件的后面追加数据,之后将文件复制到上层目录,然后将其重命名,最后再删除。
所应用到的windows函数有CreateFile, WriteFile, CopyFile, MoveFile, DeleteFile, SetFilePointer
代码
#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
HANDLE hFile = CreateFile(argv[1], GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Create failed\n");
return 0;
}
if (SetFilePointer(hFile, 0, NULL, FILE_END) == -1) {
printf("SetFilePointer error\n");
return 0;
}
char buff[] = "配置信息";
DWORD dwWrite;
if (!WriteFile(hFile, &buff, strlen(buff), &dwWrite, NULL)) {
printf("Writer error\n");
return 0;
}
printf("往%s写入数据成功\n", argv[1]);
char newCopyPath[] = "../2.txt";
char newMovePath[] = "../3.txt";
if (!CopyFile(argv[1], newCopyPath, FALSE)) {
printf("CopyFile error\n");
return 0;
}
if (!MoveFile(newCopyPath, newMovePath)) {
printf("MoveFile error\n");
return 0;
}
if (!DeleteFile(newMovePath)) {
printf("DeleteFile error\n");
return 0;
}
CloseHandle(hFile);
return 0;
}