0

I'v been doing some C++ programming I have stumbled on a weird behavior, whenever I try to get current directory using GetCurrentDirectory() it gives me back only the letter of the current disk.

Here's the code :

TCHAR path[MAX_PATH + 1] = L"";
DWORD len = GetCurrentDirectory(MAX_PATH, path);

cout << (char *)path;

Here's the output :

Z

Because my project is in my Z:\ disk

I was wondering if it has to do with the buffer size but i set it to MAX_PATH and even tried to set it to 1024.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Quentin_otd
  • 233
  • 1
  • 3
  • 16
  • 1
    What is the expected output? In which directory do you run your program? – Yksisarvinen Sep 08 '20 at 09:44
  • I tried to put it in my C:\ directory on the desktop and it retrieved only the C letter aswell, so full path is expected like C:\Users\XXX\Desktop – Quentin_otd Sep 08 '20 at 09:47
  • 2
    You compile your program in UNICODE mode. Use `wcout` instead of `cout`. And don't use the `TCHAR` type but use `w_char` instead. Actually the `(char *)` cast is the culprit here. The `TCHAR` type is only useful if you want to compiler your code as UNICODE character set program or as Multibyte character set program, latter is obsolete by now. – Jabberwocky Sep 08 '20 at 09:51
  • 1
    Your output would be better readable if you just used copy&paste to include the plain text into the question. Your screenshot seems to have choped part of the text and mixed with text in another window. – Gerhardh Sep 08 '20 at 09:57
  • @Jabberwocky I tried it and it only retrieve the letter of the disk aswell – Quentin_otd Sep 08 '20 at 09:59
  • Why are you tagging C when it's C++? – klutt Sep 08 '20 at 10:03

1 Answers1

3

The (char *) cast in cout << (char *)path makes the compiler believe that path points to a char, but it actually points to a wchar_t.

You want this (uses the multibyte character version of GetCurrentDirectory (hence the A in GetCurrentDirectory):

char path[MAX_PATH + 1] = "";
DWORD len = GetCurrentDirectoryA(MAX_PATH, path);
cout << path;

or this (recommended):

wchar_t path[MAX_PATH + 1] = L"";
DWORD len = GetCurrentDirectoryW(MAX_PATH, path);
wcout << path;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thanks it worked, didnt knew there was a Multibyte version – Quentin_otd Sep 08 '20 at 10:00
  • 1
    @Quentin_otd just forget about `TCHAR` and other `_tstrlen`, `_T("foo")` etc. They only exist so you can compile your code as UNICODE or as multibyte program. – Jabberwocky Sep 08 '20 at 10:01
  • `w_char` should be `wchar_t`, or WinAPI `WCHAR`. Also, a `MAX_PATH` buffer suffices if long paths aren't enabled because `SetCurrentDirectory` is limited to `MAX_PATH - 1` characters, including the required trailing slash. However, you should still code defensively, in case long paths are enabled for the process or there's an error. Check that the return value is greater than zero and less than `MAX_PATH`. – Eryk Sun Sep 08 '20 at 10:38
  • @ErykSun thanks, I corrected the typo. And your remark concerning long paths is correct. – Jabberwocky Sep 08 '20 at 11:10