如何改变工作目录到程序的位置 [英] how to change the working directory to the location of the program
本文介绍了如何改变工作目录到程序的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用c ++在Mac OS上打开文件。
I want to use c++ to open a file on Mac OS.
如果我在Xcode下运行程序,工作目录与程序是一样的,这很好。但是,如果我尝试在终端运行程序,工作目录是用户/用户名。你知道如何改变工作目录到程序的位置?
If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program?
以下是示例代码:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
char * dir = getcwd(NULL, 0);
cout << "Current dir: " << dir << endl;
ifstream fin("hi.txt");
if (fin.is_open()) cout << "File is Open" << endl;
else cout << "File is not open" << endl;
fin.close();
return 0;
}
推荐答案
使用值 $(PROJECT_DIR)
在您的方案调试设置的工作目录:
Use the value $(PROJECT_DIR)
in the working directory in your scheme debug settings:
这篇关于如何改变工作目录到程序的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文