4 回答

TA貢獻1757條經(jīng)驗 獲得超8個贊
你看過了getcwd()嗎?
#include <unistd.h>
char *getcwd(char *buf, size_t size);
簡單的例子:
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
int main() {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
return 0;
}

TA貢獻1833條經(jīng)驗 獲得超4個贊
#include <stdio.h> /* defines FILENAME_MAX */
//#define WINDOWS /* uncomment this line to use it for windows.*/
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
int main(){
char buff[FILENAME_MAX];
GetCurrentDir( buff, FILENAME_MAX );
printf("Current working dir: %s\n", buff);
return 1;
}
- 4 回答
- 0 關(guān)注
- 1011 瀏覽
添加回答
舉報