如何在 Windows 上使用 C 语言获取当前目录? [英] How to get current directory using C language on Windows?
问题描述
谁能告诉我如何在 Windows 上获取当前目录的路径?
Can anyone tell me how to get the path of the current directory on Windows?
推荐答案
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://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 );
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://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 );
assert( p != NULL );
printf( "The current directory is: %s", p );
free( p );
}
这篇关于如何在 Windows 上使用 C 语言获取当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!