'How to get current directory using C language on Windows?
Can anyone tell me how to get the path of the current working directory on Windows?
Solution 1:[1]
The Windows API function GetCurrentDirectory
will give you the current directory for the current process.
Alternatively, you may want to use the function getcwd
or _getcwd
, especially if you seek compatibility with POSIX platforms such as Linux.
Here is an example for using the function GetCurrentDirectory
:
#include <windows.h>
int main( void )
{
TCHAR tszBuffer[MAX_PATH];
DWORD dwRet;
dwRet = GetCurrentDirectory( MAX_PATH, tszBuffer );
if ( dwRet == 0 )
{
//TODO: handle error
}
// The buffer now contains the path of the
// current directory and can be inspected.
}
MAX_PATH
is defined when you #include <windows.h>
.
TCHAR
is just a typedef for char
if you are compiling in ASCII mode, or for a wide-character WCHAR
if you are compiling in UNICODE mode. DWORD
is just a typedef for an unsigned long
. These typedefs are declared when you #include <windows.h>
.
Here is an example for using the function getcwd
:
#include <stdio.h>
#include <direct.h>
// Microsoft wants us to use _getcwd instead of getcwd, which breaks POSIX
// compatibility. See the following link for more information:
// https://stackoverflow.com/questions/7582394/strdup-or-strdup
// Therefore we must disable the compiler warning if we want to use getcwd
// to maintain POSIX compatibility. This is accomplished with the following
// line.
#pragma warning(disable : 4996)
// We can't use the constant MAX_PATH in this program because it is not
// defined. This is because we have not included windows.h. Since MAX_PATH
// has the value 260, we will use that value.
#define BUF_SIZE 260
int main()
{
char buffer[BUF_SIZE];
char *p;
p = getcwd( buffer, BUF_SIZE );
if ( p == NULL )
{
//TODO: handle error
}
printf( "The current directory is: %s", buffer );
}
In contrast to the function GetCurrentDirectory
, the functions getcwd
and _getcwd
allow you to pass NULL
as the buffer parameter. In that case, it will allocate the memory for you with malloc
and return a pointer to that memory buffer. Therefore, you must call free
when you are finished with the buffer, to prevent a memory leak. Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
// Microsoft wants us to use _getcwd instead of getcwd, which breaks POSIX
// compatibility. See the following link for more information:
// https://stackoverflow.com/questions/7582394/strdup-or-strdup
// Therefore we must disable the compiler warning if we want to use getcwd
// to maintain POSIX compatibility. This is accomplished with the following
// line.
#pragma warning(disable : 4996)
int main()
{
char *p;
p = getcwd( NULL, 0 );
if ( p == NULL )
{
//TODO: handle error
}
printf( "The current directory is: %s", p );
free( p );
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |