且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Windows 上使用 C 语言获取当前目录?

更新时间:2021-11-09 01:46:52

Windows API 函数 GetCurrentDirectory 将为您提供当前进程的当前目录.

The Windows API function GetCurrentDirectory will give you the current directory for the current process.

或者,您可能希望使用函数 getcwd_getcwd,特别是如果您寻求与 Linux 等 POSIX 平台的兼容性.

Alternatively, you may want to use the function getcwd or _getcwd, especially if you seek compatibility with POSIX platforms such as Linux.

以下是使用函数 GetCurrentDirectory 的示例:

Here is an example for using the function GetCurrentDirectory:

#include <windows.h>
#include <assert.h>

int main( void )
{
    TCHAR tszBuffer[MAX_PATH];
    DWORD dwRet;

    dwRet = GetCurrentDirectory( MAX_PATH, tszBuffer );
    assert( dwRet != 0 );

    // The buffer now contains the path of the
    // current directory and can be inspected.
}

MAX_PATH 是在您 #include 时定义的.

TCHAR 只是 char 的 typedef,如果你在 ASCII 模式下编译,或者宽字符 WCHAR 如果你正在编译在 UNICODE 模式下.DWORD 只是 unsigned long 的 typedef.这些 typedef 在您 #include #include 时声明.

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>.

这是一个使用函数getcwd的例子:

Here is an example for using the function getcwd:

#include <stdio.h>
#include <direct.h>
#include <assert.h>

// Microsoft wants us to use _getcwd instead of getcwd, which breaks POSIX
// compatibility. See the following link for more information:
// https://***.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 );
    assert( p != NULL );

    printf( "The current directory is: %s", buffer );
}

与函数 GetCurrentDirectory 不同,函数 getcwd_getcwd 允许您将 NULL 作为缓冲区参数.在这种情况下,它将使用 malloc 为您分配内存并返回指向该内存缓冲区的指针.因此,您必须在完成缓冲区后调用 free,以防止内存泄漏.下面是一个例子:

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>
#include <assert.h>

// Microsoft wants us to use _getcwd instead of getcwd, which breaks POSIX
// compatibility. See the following link for more information:
// https://***.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 );
    assert( p != NULL );

    printf( "The current directory is: %s", p );

    free( p );
}