File I / O from the current position of Windows C ++

I have not yet found a definitive answer about this. I am trying to access files in subfolders from my .EXE. When I asked before, people tell me to use an absolute location, that is, "c: / game / info /" if I wanted to access anything in / info /

But for me or someone it is completely unreasonable to assume that someone is going to use their program from the same directory. What if the user has only a D-disk? That kind of thing.

So my question is: how can I access the file in a subdirectory from my executable without relying on the whole path?

+3
source share
4 answers

"Windows", WinAPI.

Windows GetModuleFileName(NULL, ...) PathRemoveFileSpec. PathAppend .

.exe Win32, .


, . , ACL ( ).

+8

GetModuleFileName ( , . .)

char strExePath [MAX_PATH];
GetModuleFileName (NULL, strExePath, MAX_PATH);

(- , ) .

+4

Create or use an installer that asks the user where to install the executable file and writes this registry path in a known place for later reference.

0
source

if you use:

#include <fstream>

ifstream stream("file");

he will work. "file" is the file in the directory with your exe. Of course, if you want to go up or down the folder hierarchy, use ".. \ file" or "folder \ file"

0
source

All Articles