Sometimes _getcwd() returns the parent direcory of the current one, other times returns the folder of the current user.. Has it ever happened to you-all? Thanks in advance

I suspect that you are changing the current working directory without realising it. Try experimenting a bit to see what it is you are doing before you call _getcwd() which might be changing it.

I suspect that you are changing the current working directory without realising it. Try experimenting a bit to see what it is you are doing before you call _getcwd() which might be changing it.

Thank you for your reply. The call to _getcwd is at the very beginnning of the main():

#include <windows.h> 
#include <iostream> 
#include <string.h>
#include <string>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main()
{
    HANDLE hFind;
    WIN32_FIND_DATA FindData;

   char * curWorkDir=new char[MAX_PATH];

   if( (curWorkDir = _getcwd( NULL, 0 )) == NULL )
      perror( "_getcwd error" );
...

Thank you for your reply. The call to _getcwd is at the very beginnning of the main():

Fair enough.
How are you invoking the program? The cwd is not necessarily the directory containing the executable, so if you invoke it in different ways this may affect the cwd path you are given.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Fair enough.
How are you invoking the program? The cwd is not necessarily the directory containing the executable, so if you invoke it in different ways this may affect the cwd path you are given.

I just double-click on the executable or I run it from VS if I have just modified some line..

You could try ::GetCurrentDirectory() and see if it behaves differently, though I doubt it.
If you are actually interested in the directory in which the executable is installed, as opposed to the current working directory, then _getcwd() is not what you want anyway. In this case you will get the correct path from ::GetModuleFileName().

I just double-click on the executable or I run it from VS if I have just modified some line..

Those two ways will generally give different results.

Give us more data and we can give you a complete answer.

The working (or current) directory does not bear a relation to the starting executable module placement - that's the answer to OP question.

I tried GetCurrentDirectory, but the behavior is the same. GetModulePath returns the exe name also, not a big problem but it needs some instruction more. Thank you

It seems you misunderstand the notion of a current working directory. That's why you think that GetCurrentDirectory (or _getcwd) has the behavior. They have not. These functions (more precisely, it's GetCurrentDirectory only on Windows because non-standard RTL function _getcwd calls it on Windows) return the correspondent current process property which OS assigned to it at the start. There are lots of methods to set this property. Moreover, the process can reassign its current working directory via SetCurrentDirectory call as desired.

Therefore the current directory of your executable module run does not depend on the module itself. That's why your studies of _getcwd behavior are senseless.

This article has been dead for over six months. Start a new discussion instead.