#ifdef _WIN32
# include <direct.h>
# define getcwd _getcwd // stupid MSFT "deprecation" warning
#else
# include <unistd.h>
#endif /* _WIN32 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
char *buf = getcwd(NULL, 0);
// A NULL return value indicates an error, and <b>errno</b> is set either
// to ENOMEM, or to ERANGE
if (NULL == buf) {
printf ("errno: %d -- %s\n", errno, strerror(errno));
exit(1);
}
string current_dir(buf);
free(buf);
printf("current working directory: %s\n", current_dir.c_str());
return 0;
}
函数说明可参考 c语言 getcwd() 函数