Search code examples
c++iosxcodecharfstream

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:

hello world!

However, when I run this on Xcode, I get an inverted question mark as the output. It works fine on terminal, but not on 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.


Solution

  • Your text.txt file will not be at the executable path.

    Go to your Build Phases - > Copy Files -> Add Your text file

    Copy File Phase

    Make sure that:

    • Destination should be Products Directory
    • Copy only when installing should be unchecked