Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Adding a file to run with a command line tool

Is there a way to have xcode 5 run a command line tool program with a file that the program can pull data from to use in a cin or scanf function? I'm thinking of what can be done in the command line when once a program is compiled you can have run "myfile <infile.txt" and your program will run taking input data from infile.txt rather than the keyboard.

Posted on Mar 7, 2014 8:56 AM

Reply
11 replies

Mar 7, 2014 9:29 AM in response to vajra78

You want what to take input from a file, a program running on the command line, then


# program < file


If you want to do this from inside another program; you first create the child process, open the file you want read on stdin, then use dup2() to duplicate the descriptor on fd 0 (stdin) and then exec the program


More then this and you'll have to show your code, what you have so far. I hate doing home work assignments 😉

Mar 7, 2014 9:39 AM in response to Frank Caggiano

#program <file


Yes exactly, but I don't want to use the command line. I am just writting c and c++ programs with the command line tool template in xcode and debugging, compiling, linking and running within xcode too. When I click run in xcode I want it to do #program <file and show me the out put right there in the Output box. I don't want to go back and fourth between a text editor (or xcode) and the command line.

Mar 7, 2014 10:36 AM in response to etresoft

@etersoft, Wasn't sure what to do in there. Would that be a post run action or pre run. Then would I just use "</infile.txt " for the sricpt? Is it ok to put the infile.txt in the same folder as main? Last time I did that and ran from the command line something weird happend. The program ran with the input.txt file but then main got overwritten with the input.txt file data. Guess I'll just try stuff. My command line skills are lacking but you don't get better if you don't try. Need to take a unix os class.🙂

Mar 7, 2014 10:41 AM in response to etresoft

Glad you jumped in.


I do seem to remember in Xcode 4 being able to, in the run scheme, add a file redirect to the arguments passed on launch.


But I've been trying what I think was what I did in 4, adding < file to the argument line and it's not working.


I tried quoting the string, giving absolute paths, but no matter what the program just sees it as a command line argument the file is not being redirected.

User uploaded file

neither work. The first sees agrv 1 as the whole line the second sees argv 1 as <


So am I missing something? Did they change this in 5?

Mar 7, 2014 3:15 PM in response to vajra78

What you have to do is change your executable path to /bin/sh or something similar. It has to be an executable, not a script. You can then pass a script as an argument if you want. I just tried a little project with /bin/sh as my executable and my arguments passed on launch are:


/Users/jdaniel/bin/tester | $(TARGET_BUILD_DIR)/$(TARGET_NAME)


tester is a little script that echoes a line. Then that gets piped to my Xcode program. I could probably hack on that a bit to get it to expand shell variables like ~ so I wouldn't have to pass my home directory. It will expand Xcode environment variables. You would have to use those as the derived paths aren't easily guessed.

Mar 9, 2014 7:13 PM in response to etresoft

Ok, got it working with help from another forum. I think what I wanted to do was a little different. After setting the excuitable to /bin/sh in Edit Schemes I used the following argument and it worked:


-c "$(TARGET_BUILD_DIR)/$(TARGET_NAME) < /filepath/infile.txt"


Note in edit schemes to set the excuitable I had to use cmd+shft+g to get the goto drop down menu a then search for /bin/sh. Also the argument didn't like a long file path for the infile for some reason.


Thanks all for the help.

Adding a file to run with a command line tool

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.