File Path

May 31, 2011 at 11:48pm
I need a code to find the path of a file that the folder is in. Doesn't the
__LINE__ give you the
C:\Users\CPlusPlus\Desktop\CPlusPlusDemos\C++.cpp
, but how do you get just the folder path?

Jun 1, 2011 at 1:52am
You mean __FILE__.
See http://www.cplusplus.com/reference/string/string/find_last_of/ and the example in particular.

But I hope you realize that __FILE__ will give you the path to the current source file at the time of compilation - it has nothing to do with the path the executable is run from.
Jun 1, 2011 at 6:54am
Use PathRemoveFileSpec() WinAPI to extract folder path from full path.
Jun 2, 2011 at 12:41pm
Can you show me an example of it?
Jun 4, 2011 at 4:31am
1
2
3
4
#include <shlwapi.lib>

TCHAR path[] = _T("C:\Users\CPlusPlus\Desktop\CPlusPlusDemos\C++.cpp");
PathRemoveFileSpec(path); // path will now contains C:\Users\CPlusPlus\Desktop\CPlusPlusDemos 
Jun 5, 2011 at 1:29am
Thanks. Just a couple things please. Can you explain the #include <shlwapi.lib> , and what's TCHAR?
Jun 5, 2011 at 4:45am
There is a typo,should be 'shlwapi.h'.
TCHAR is a macro that resolves to char or wchar_t, depending on UNICODE is defined or not
Topic archived. No new replies allowed.