简体   繁体   English

Xcode从C ++文件中读取字符

[英]Xcode reading chars from file in c++

I have the following code to read a character from a file: 我有以下代码从文件中读取字符:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, const char * argv[])
{
    ifstream f("text.txt");
    char c;
    f.get(c);
    cout << c << endl;


    return 0;
}

and my text.txt file contains: 我的text.txt文件包含:

hello world!

However, when I run this on Xcode, I get an inverted question mark as the output. 但是,当我在Xcode上运行它时,我得到一个反向的问号作为输出。 It works fine on terminal, but not on Xcode. 它可以在终端上正常工作,但不能在Xcode上工作。 Does anyone know why this happens? 有谁知道为什么会这样吗?

I'm using Xcode to debug some code, but I cant do that anymore because this problem is causing a lot of other errors in my program. 我正在使用Xcode调试某些代码,但是我不能再这样做了,因为此问题在程序中引起了许多其他错误。

Your text.txt file will not be at the executable path. 您的text.txt文件将不在可执行文件路径中。

Go to your Build Phases - > Copy Files -> Add Your text file 转到Build Phases -> Copy Files ->添加文本文件

复制文件阶段

Make sure that: 确保:

  • Destination should be Products Directory 目的地应该是产品目录
  • Copy only when installing should be unchecked 仅在安装时取消选中复制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM