使用C/C++怎麼獲取當前運行程序的運行根目錄:
Linux(Unix): c++
函數原型 函數
#include <unistd.h> char* getcwd(char *buf, size_t size);
// VC++ 6.0 #include <stdio.h> #include <direct.h> #include <stdlib.h> int main( int argc,char *argv[] ) { char path[_MAX_PATH]; _getcwd(path,_MAX_PATH); printf("當前工做目錄:\n%s\n",path); if( ( _chdir("d:\\visual c++") ) == 0 ) { printf("修改工做路徑成功\n"); _getcwd(path,_MAX_PATH); printf("當前工做目錄:\n%s\n",path); } else { perror("修改工做路徑失敗"); exit(1); } return 0; } // vs2008 #include <direct.h> #include <stdlib.h> #include <stdio.h> int main( void ) { char* buffer; // 獲得當前的工做路徑 if( (buffer = _getcwd( NULL, 0 )) == NULL ) { perror( "_getcwd error" ); } else { printf( "%s \nLength: %d\n", buffer, strnlen(buffer) ); free(buffer); } }