我创建了一个包含内容的文本文件.它与 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模板网!
在 C++ 中转置矩阵的最快方法是什么?What is the fastest way to transpose a matrix in C++?(在 C++ 中转置矩阵的最快方法是什么?)
使用 boost 或 STL 在 C++ 中对压缩(锁定)容器进行排Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中对压缩(锁定)容器进行排序)
围绕另一个点旋转一个点 (2D)Rotating a point about another point (2D)(围绕另一个点旋转一个点 (2D))
图像处理:'Coca-Cola Can' 识别的算法改进Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(图像处理:Coca-Cola Can 识别的算法改进)
如何在 C++ 中构建 ISO 8601 日期时间?How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中构建 ISO 8601 日期时间?)
使用 STL 排序功能对列表进行排序Sort list using STL sort function(使用 STL 排序功能对列表进行排序)