RAD Studio
ContentsIndex
PreviousUpNext
getcwd, _wgetcwd

Header File 

dir.h  

Category 

Directory Control Routines 

Prototype 

char *getcwd(char *buf, int buflen); 

wchar_t *_wgetcwd(wchar_t *buf, int buflen); 

Description 

Gets current working directory. 

getcwd gets the full path name (including the drive) of the current working directory, up to buflen bytes long and stores it in buf. If the full path name length (including the null terminator) is longer than buflen bytes, an error occurs. 

If buf is NULL, a buffer buflen bytes long is allocated for you with malloc. You can later free the allocated buffer by passing the return value of getcwd to the function free. 

Return Value

  • getcwd returns the following values:
  • If buf is not NULL on input, getcwd returns buf on success, NULL on error.
  • If buf is NULL on input, getcwd returns a pointer to the allocated buffer.
In the event of an error return, the global variable errno is set to one of the following values:  

ENODEV 
No such device 
ENOMEM 
Not enough memory to allocate a buffer (buf is NULL) 
ERANGE  
Directory name longer than buflen (buf is not NULL) 

Example

#include <stdio.h>
#include <dir.h>
int main(void)
{
   char buffer[MAXPATH];
   getcwd(buffer, MAXPATH);
   printf("The current directory is: %s\n", buffer);
   return 0;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
getcwd 
 
 
_wgetcwd 
 
NT only 
 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!