Reputation: 49
I'm trying to read file from the directory the exe file is located. The data.txt file is in VS Project directory and when I specify the full path everything works fine.
char curDirectory[MAX_PATH];
GetCurrentDirectory(MAX_PATH, curDirectory);
char filePath[MAX_PATH];
char *name = "\\data.txt";
memcpy(filePath, curDirectory, sizeof(curDirectory));
memcpy(filePath + strlen(curDirectory), name, strlen(name));
HANDLE hFile = CreateFile(filePath, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Upvotes: 0
Views: 1209
Reputation: 613572
You don't null terminate the string. Do so by passing strlen(name) + 1
in the second call to memcpy
.
Some other observations:
CreateFile
fails, you should call GetLastError
to obtain an error code. strcpy
and strcat
rather than memcpy
when working with strings. std::string
and have that class manage buffers. Upvotes: 0