解释我的问题的最好方法可能就是向您展示我的代码,因为它非常简单。
#include <iostream>
#include <fstream>
int main (int argc, const char * argv[])
{
std::ifstream in;
std::string line;
in.open("test.txt");
if (in.fail()) std::cout << "failed. \n";
getline(in, line);
std::cout << line;
return 0;
}
所以当我运行它时,控制台返回“失败”。而不是打开名为 test.txt 的文件——它与我的 .xcodeproj 文件位于同一文件夹中,并且还显示在我的 Xcode 导航器中。
我不确定我对这个过程有什么误解,但我怀疑它会很简单。
谢谢! :)
最佳答案
该文件与您的 .xcodeproj 文件位于同一目录中?那么,您的问题就在这里。
默认情况下,从 Xcode 启动的进程的工作目录将是输出目录(即程序所在的目录)。根据您的 Xcode 版本,它可能位于 <Project Directory>/build/Debug
中。 .
尝试将文件移到那里。
关于c++ - 将 .open 与 ifsteam 对象一起使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8612286/