Click here to Skip to main content
Sign Up to vote bad
good
See more: C++Windows
Hy
I need to save a text file in current directory in project.
how to get path of this file ?
thank you
Posted 12 Sep '12 - 20:14
Edited 12 Sep '12 - 21:55


3 solutions

  Permalink  
Comments
khosro goudarzi - 13 Sep '12 - 2:18
Thanks for your help but I want to get path of fa file in win32 project (c++) .
pasztorpisti - 13 Sep '12 - 3:47
Then you should have added the Windows tag to your question or you should have mentioned that.
Use GetModuleFileName()[^].
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
}
  Permalink  
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 611
1 Maciej Los 265
2 Slacker007 240
3 CPallini 235
4 OriginalGriff 210
0 Sergey Alexandrovich Kryukov 9,118
1 OriginalGriff 7,134
2 CPallini 3,803
3 Rohan Leuva 3,135
4 Maciej Los 2,558


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid