C++ 使用.openwithifsteam对象时出现问题

C++ 使用.openwithifsteam对象时出现问题,c++,io,C++,Io,解释我的问题的最好方法可能就是向您展示我的代码,因为它非常简单 #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";

解释我的问题的最好方法可能就是向您展示我的代码,因为它非常简单

#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;
}
#包括
#包括
int main(int argc,const char*argv[]
{
std::ifstream-in;
std::字符串行;
in.open(“test.txt”);

如果(in.fail())std::cout文件与.xcodeproj文件位于同一目录中?那么,这里就是您的问题

默认情况下,从Xcode启动的进程的工作目录将是输出目录(即程序所在的目录)。根据您的Xcode版本,它可能位于
/build/Debug


请尝试将文件移到那里。

该文件与.xcodeproj文件位于同一目录中?好吧,您的问题就在这里

默认情况下,从Xcode启动的进程的工作目录将是输出目录(即程序所在的目录)。根据您的Xcode版本,它可能位于
/build/Debug


尝试将文件移动到那里。

Woohoo!这是解决方案,谢谢!文件夹名称最终为:/Users/username/Library/Developer/Xcode/DerivedData/project\u dir/Build/Products/debugh。在Xcode 4:Woohoo中找到了一个创建输出目录的好参考:这是解决方案,谢谢!文件夹名称最终为:/Users/username/Library/Developer/Xcode/DerivedData/project_dir/Build/Products/debugh找到了一个关于在Xcode 4中创建输出目录的好参考: