【问题标题】:Xcode 5 c++ fstream (ifstream and ofstream) do not workXcode 5 c++ fstream(ifstream 和 ofstream)不起作用
【发布时间】:2013-11-12 04:42:40
【问题描述】:

所以我正在尝试读取我的应用程序内容文件夹中的文件中的内容:

std::string username;
std::ifstream user("../Resources/username.txt");
user >> username;
user.close(); /*here I do a breakpoint to check the value of username and it's ""*/

我做了一个“if”来检查文件是否打开,显然不能。你告诉我我做错了什么(我知道文件路径和它的名称是正确的

【问题讨论】:

  • 您在 std::ifstream user("../Resources/username.txt", std::ifstream::in) 中缺少“std::ifstream::in”;除此之外,您的示例在 VC++ 中对我有用
  • 我会在 xcode 完成更新后立即对其进行测试,但我应该为 ofstream 放置什么? std::ofstream::out/in?
  • ifstreamofstream 默认分别为 in 和 out,所以这不是问题。您确定文件实际上正在打开吗?你从不检查。
  • 好吧,我测试了它并且完全相同的问题。正如我之前所说,我测试了文件是否打开并且没有
  • 好吧,fstream 没有损坏,您的代码看起来还可以,所以唯一剩下的就是文件不在您认为的位置。再仔细看看你的目录结构,看看哪里出了问题。

标签: c++ fstream xcode5.0.1


【解决方案1】:

试试这个:

std::string username;
std::ifstream user("C://test.txt", std::ifstream::in);

user >> username;

user.close(); /*here I do a breakpoint to check the value of username and it's ""*/

【讨论】:

  • std::ifstream::beg?
  • 必须是你的路径然后你应该至少读取文件中的第一个字符串
  • 这条路径适用于其他东西,如图像、歌曲等(我使用 SDL2),这就是为什么它让我如此困惑
  • 如果您使用的是 Xcode,您将无法从驱动器 C 读取文件
猜你喜欢
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多