Redirecting I/O in Xcode 4

后端 未结 1 485
小鲜肉
小鲜肉 2021-01-03 22:42

I just installed Xcode 4 and I\'m trying to redirect input from a file to my C++ program. I\'ve tried using the usual \"< infile.txt\" in the \"Arguments\" section of my

1条回答
  •  不知归路
    2021-01-03 23:18

    I tested with various types of Arguments and it appears that Xcode have a bug with Arguments (last test: Xcode 8).

    But there is one alternative to simulate with similar effect. You must use the Environment Variables.

    Add a Environment Variable with the file name you want to redirect:

    enter image description here

    Then in your code you must "redirect" this file to the standard input (cin):

    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main (int argc, const char * argv[])
    {
        ifstream arq(getenv("MYARQ"));
        cin.rdbuf(arq.rdbuf());
    
        string value;
        cin >> value;
        cout << value;
    
        return 0;
    }
    

    that's it... only 2 lines of code

    ifstream arq(getenv("MYARQ"));
    cin.rdbuf(arq.rdbuf());
    

    it's not the best solution, but while xcode have this problem this is the only solution !

    0 讨论(0)
提交回复
热议问题