C ++ CreateFile did not find .txt file in same folder as .exe

I am trying to use a function CreateFile, but it is not as planned.

I made 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 create .exeand put the file test.txtin the same folder.

When I perform .exe, I get getLastError() = 2what it meansERROR_FILE_NOT_FOUND

How is this possible?

+4
source share

Source: https://habr.com/ru/post/1680835/


All Articles