我创建了一个包含内容的文本文件.它与 cpp 文件位于同一文件夹中.我已经多次确认该文件存在.当我运行 g++ 时,编译并运行它会找到该文件.当我在 Xcode 中运行它时,它不起作用.如果找不到文件.
I have created a text file with content. It is located in the same folder as the cpp files. And I have confirmed several times that the file exists. When I run g++, compile and run it finds the file. When I run it in Xcode, it does not work. If fails to find the file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
您的文件无法打开,因为 XCode 从 IDE 中的默认 build 位置启动,该位置实际上是某个位置的临时目录你的磁盘.如果您想在启动时将工作目录更改为其他内容(例如可以找到文件的位置):
Your file fails to open because XCode launches from the IDE in the default build location, which is actually a temporary directory off somewhere on your disk. If you want change the working directory at launch-time to something else (like the location where your files can be found):
如果您没有看到,这也是您可以配置同一对话框的命令行参数(在另一个选项卡上)的地方.
And in case you didn't see it, this is also the place where you can configure command-line arguments (on another tab) of the same dialog.
这篇关于代码在 g++ 中运行完美,但在 Xcode 中却不是 - 找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!