Share this page

# getcwd

File Manipulation Functions

Returns the name of the current working directory.

    char *getcwd( char *path, int numchars);

path | The path name.
numchars | The maximum number of characters to retrieve.

For getcwd details, refer to your C language documentation.

## Return Values

This function returns zero upon success and (-1) for failure.

## Example

This example uses getcwd to save the current directory, before moving to a new directory. It then moves back to the first directory.

        #include <direct.h>

        #define DIR_LEN 512
        char original_dir[DIR_LEN];
        #ifdef unix
            char new_dir[] = "/tmp";
        #else
            char new_dir[] = "C:\\";
        #endif

        if (!getcwd(original_dir, DIR_LEN)) {
            lr_output_message ("getcwd error");
            return -1;
        }

        // Change the current working directory to new_dir

        if (chdir(new_dir)) {
            lr_output_message ("Unable to locate directory: %s", new_dir);
            return -1;
        }
        else
            lr_output_message ("Changed dir to %s", new_dir);

        // Move back to original directory

        if (chdir(original_dir)) {
            lr_output_message ("Cannot move back to %s", original_dir);
            return -1;
        }
        else
            lr_output_message ("Moved back to %s", original_dir);

    Example: Output:
    Action.c(25): Changed dir to C:\
    Action.c(35): Moved back to D:\TEMP\noname13

Tips to improve your search results... VuGen Function Reference Version 26.1 Function Reference 26.1 All View Local Help Center View Online Help Center Version: