Windows編程之文件操作



歡迎關(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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容