Xcode:将输入文件放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23495746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode: Where to put input file?
提问by MayNotBe
I'm trying to learn C++ and I'm using Xcode. I have the following main
method:
我正在尝试学习 C++,我正在使用 Xcode。我有以下main
方法:
int main()
{
const int SIZE = 256;
Expression* expression;
char paren, comma, line[SIZE];
ifstream fin("input.txt");
while (true)
{
symbolTable.init();
fin.getline(line, SIZE);
if (!fin)
break;
stringstream in(line, ios_base::in);
in >> paren;
cout << line << " ";
expression = SubExpression::parse(in);
in >> comma;
parseAssignments(in);
double result = expression->evaluate();
cout << "Value = " << result << endl;
// catch the exceptions
return 0;
}
}
Where do I put the file "input.txt"
so the program can read it?
我把文件放在哪里"input.txt"
以便程序可以读取它?
回答by eerorika
The filename parameter of ifstream
is usually taken as a relative path to the working directoryso that's where you should put the file.
的文件名参数ifstream
通常用作工作目录的相对路径,因此您应该将文件放在那里。
If you launch the executable from a file manager, the working directory of the process will most likely be set to the directory the executable is in. In that case the text file should be in the same directory.
如果您从文件管理器启动可执行文件,则进程的工作目录很可能会设置为可执行文件所在的目录。在这种情况下,文本文件应位于同一目录中。
回答by molbdnilo
All relative paths (on OS X, any path that doesn't start with a slash, "/"), are interpreted relative to a process' working directory.
所有相对路径(在 OS X 上,任何不以斜杠“/”开头的路径)都相对于进程的工作目录进行解释。
If you're running from the terminal, it should be in the terminal's current directory (i.e. ls
should list it).
如果您从终端运行,它应该在终端的当前目录中(即ls
应该列出它)。
If you're running from inside XCode, there is a project setting for which directory should be the working directory.
You set that to wherever your file is, or move the file to wherever that directory is.
如果您从 XCode 内部运行,则有一个项目设置,哪个目录应该是工作目录。
您可以将其设置为文件所在的任何位置,或者将文件移动到该目录所在的任何位置。