How to get the current directory Using a C program



 how to get the current directory Using a c-program

Learn C - C tutorial - how to get the current directory Using a c-program - C examples - C programs

How to get the current directory Using a C program

  • C Program to print current working directory and to print the path of current working directory.
  • The code below is applicable for both Linux and window. we just need to define the flags for current operating system.
  • By default the code will run as it is on Linux.

Sample Code

#include <stdio.h>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDirgetcwd
#endif
int main()
{
    char buff[FILENAME_MAX];
    GetCurrentDir( buff, FILENAME_MAX );
    printf("Current working dir: %s\n", buff);
    return 1;
}

Output

Current working dir: /home/cg/root/2812814


View More Quick Examples


Related Searches to How to get the current directory Using a C program