GetCurrentDirectory和SetCurrentDirectory

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


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值