Click here to Skip to main content
12,348,451 members (69,674 online)
Rate this:
 
Please Sign up or sign in to vote.
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:14pm
Edited 12-Sep-12 21:55pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100