Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting I/O in Xcode 4

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 Run scheme, but that didn't work. I was able to redirect input and output in Xcode 3 just fine (by editing the arguments for the executable). Any suggestions on how to do this in the new version?

Thanks!

Samer

like image 709
Samer Avatar asked Mar 30 '11 17:03

Samer


People also ask

What is stdin Macos?

stdin: The standard input pipe is where a command receives input. By default, you enter input from the command-line interface. You can redirect the output from files or other commands to stdin.

How do I redirect in Mac terminal?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.


1 Answers

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 <iostream>
#include <string>
#include <fstream>

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 !

like image 84
Farlei Heinen Avatar answered Nov 15 '22 13:11

Farlei Heinen