0

I wanted to obtain the PATH of Current Working Directory in C++ program. It should run for Windows, Sun Solaris, UNIX. I found this to be my answer. But our compiler is giving error that it cannot find the file unistd.h. I am compiling in Windows. I have used the exact same code as mentioned in the correct answer. What is the problem here?

Plus my boss is questioning the working of this code. I have commented the UNIX lines & the program is running fine in Windows. Even he does not know how this code is working. Can anybody please explain this code to me please? Thank You.

Community
  • 1
  • 1
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • Read the linked answer again, and ask yourself: does your Windows build define the WINDOWS symbol so that the right includes are used? `unistd.h` doesn't exist on Windows and the code is fairly explicit about not wanting to include it, it uses `_getcwd` from direct.h instead. – Matthew Walton Mar 11 '13 at 08:40

1 Answers1

1

<unistd.h> is not supposed to be found on Windows. The file to be included in Windows according to How do I get the directory that a program is running from? is <direct.h>. Check your preprocessor macros.

Community
  • 1
  • 1
uba
  • 2,012
  • 18
  • 21
  • actually I have added them in a function where i want the path. Is it ok? or should add it globally? – Cool_Coder Mar 11 '13 at 08:41
  • @CAD_coding You can't include headers in a function, so what exactly are you doing? The code assumes `WINDOWS` is defined when compiling under Windows, so check your preprocessor variable to make sure it is defined there. – Voo Mar 11 '13 at 08:42
  • The lines starting from `#ifdef` till the `#endif` line should be added at the top, not in the function – uba Mar 11 '13 at 08:42
  • Works like a charm on Windows! – Cool_Coder Mar 11 '13 at 09:22