GetCurrentDirectory和SetCurrentDirectory是获取和设置的线程的当前工作路径,而不是线程所在路径。下面的代码是源文件所在路径下有一个文件为write.txt,而上一级路径下没有这个文件,来说明这两个函数的功能。
#ifndef _TEST_H
#include <iostream>
#include <windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
char buf[1000];
int i=1000;
GetCurrentDirectory(1000,buf); //得到当前工作路径
string a;
a.assign(buf);
cout<<a<<endl;
a.append("\\..\\");
FILE *pFile;
pFile = fopen("write.txt","r");
if (pFile == NULL)
cout << "open failure" << endl;
fclose(pFile);
int test = SetCurrentDirectory(a.c_str()); //设置当前路径值
if (test == 0 )
cout << "execute failed\n";
GetCurrentDirectory(1000,buf);
cout << buf << endl;
pFile = fopen("write.txt","r");
if (pFile == NULL)
cout << "open failure" << endl;
else
fclose(pFile);
return 0;
}
#endif //_TEST_H