个人认为,getcwd是最方便的方法,只需要包涵头文件<direct.h>即可.
获取当前目录方法还可以选用GetCurrentDirectory和GetModuleFileName,但经测试,getcwd更适合一些。
方法:包含头文件#include <direct.h> 然后就可以使用getcwd(char *buffer, int maxlen)方法来获取当前目录了
测试程序如下:
(如有不懂,可以访问:http://msdn.microsoft.com/zh-cn/library/sf98bd4y(v=VS.80).aspx)
#include <iostream>
#include <string>
#include <direct.h>//---getcwd
#include <Windows.h>
using namespace std;
#define MAX_PATH 260 //define in windows.h
int main()
{
string str;
char ch[MAX_PATH];
getcwd(ch,MAX_PATH);//ok...
str=ch;
cout<<str;
//getcwd(str,MAX_PATH);//error..>不能将参数 1 从“std::string”转换为“char *”
cout<<"/ngetcwn:"<<endl;
cout<<ch<<endl;
//GetCurrentDirectory(MAX_PATH,str);//error..>不能将参数 2 从“std::string”转换为“LPWSTR”
//GetCurrentDirectory(MAX_PATH,ch);//error..>不能将参数 2 从“char [260]”转换为“LPWSTR”
//GetModuleFileName(NULL,str,MAX_PATH);//error..>不能将参数 2 从“std::string”转换为“LPWCH”
//GetModuleFileName(NULL,ch,MAX_PATH);//error..>不能将参数 2 从“char [260]”转换为“LPWCH”
//GetModuleFileName();
return 0;
}