C++ CreateFile does not found .txt file in same folder as .exe [closed]

只愿长相守 提交于 2019-12-22 09:39:17

问题


I'm trying to use CreateFile function, but it does not go as planned.

I did a simple test code :

#include <Windows.h>
#include <iostream>
#include <tchar.h>

using namespace std;

int main() {
    HANDLE hFile;
    hFile = CreateFile(_T("test.txt"), GENERIC_READ, NULL, NULL, OPEN_EXISTING, NULL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        cout << GetLastError() << endl;
        Sleep(2000);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

I generate the .exe and place a file test.txt in the same folder.

When I execute the .exe I get getLastError() = 2 which means ERROR_FILE_NOT_FOUND

How is it even possible?

来源:https://stackoverflow.com/questions/44935386/c-createfile-does-not-found-txt-file-in-same-folder-as-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!