Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hy I need to save a text file in current directory in project.
how to get path of this file ?
thank you
Posted
Updated 12-Sep-12 21:55pm
v2

TCHAR szExe[MAX_PATH];
GetModuleFileName(NULL, szExe, sizeof szExe / sizeof TCHAR);
szExe[strrchr(szExe, '\\') - szExe + 1] = '\0';

if the EXE file is C:\Window\test.exe, then the value of szExe is "C:\\Windows\".You can see there is a '\' at last.
 
Share this answer
 
Use GetModuleFileName()[^].
C++
char exe_path[MAX_PATH];
if (DWORD len = GetModuleFileName(NULL, exe_path, sizeof(exe_path)/sizeof(exe_path[0])))
{
    // removing the filename of the exe, leaving there just the directory part...
    while (len>0 && exe_path[len-1]!='\\')
        --len;
    exe_path[len] = 0;
    // now exe_path holds the directory that contains the exe file with a trailing backslash
    strcat(exe_path, "myfile.txt");
    // blah blah blah
}
 
Share this answer
 
v2
 
Share this answer
 
v2
Comments
khosro goudarzi 13-Sep-12 2:18am    
Thanks for your help but I want to get path of fa file in win32 project (c++) .
pasztorpisti 13-Sep-12 3:47am    
Then you should have added the Windows tag to your question or you should have mentioned that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900