歡迎關(guān)注我的新博客 https://pino-hd.github.io,最新的博文都會發(fā)布在上面哦~
Windows編程之文件操作
該程序主要功能就是創(chuàng)建一個文件,然后在文件的后面追加數(shù)據(jù),之后將文件復(fù)制到上層目錄,然后將其重命名,最后再刪除。
所應(yīng)用到的windows函數(shù)有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寫入數(shù)據(jù)成功\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;
}