char buffer[MAX_PATH]; _getcwd(buffer, MAX_PATH); printf ( "The current working directory is: %s" , buffer); return 0 ;

代码中的 _getcwd 函数可以用来获取当前目录。它需要两个参数:一个字符数组用来存储当前目录的路径,以及这个数组的最大长度。

你也可以使用 GetCurrentDirectory 函数来获取当前目录。它的用法与 _getcwd 函数类似,但是它返回值是一个布尔值,表示是否成功获取到了当前目录。

#include <windows.h>
#include <stdio.h>
int main()
    char buffer[MAX_PATH];
    if (GetCurrentDirectory(MAX_PATH, buffer))
        printf("The current working directory is: %s", buffer);
        printf("Failed to get the current working directory.");
    return 0;

注意:在使用这些函数之前,你需要在程序的头文件中包含 direct.hwindows.h

  •