Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alexWoz

macrumors newbie
Original poster
Sep 12, 2009
13
0
Quick question..

I'm trying to replicate functionality of the following command in xcode:

./program < test.txt

So when my program says "cin >> stuff" it grabs the text from test.txt. Is there any way to do this in xcode using ifstream?
 
Actually it seems more like an incomplete question.

Do you already have an Xcode project?

If yes is it of type 'Command Line Tool'?

If it is select "Edit Active Executable <your project name>" from the "Project" menu in Xcode. A dialog titled "Executable <your project name> Info" will appear. Select the "General" tab and you will be presented options concerning your executables "current" directory. This will be the directory in which your "test.txt" input file exists.

Additionally the "Console" item in the "Run" menu will bring a console window up allowing you to interact with your executable during development.

Don't read anything as to the tone of my response as I'm simply not feeling well.
 
Thank you for dealing with my incompetence. So I had the test.txt file in the correct directory. Is there any way I can tell my program (as it executes) to use the content within test.txt as the user input? So if, for example, my program is as follows:

Code:
int main(int argc, char ** argv) {

string num1; string num2;
cin >> num1 >> num2;
cout << num1 << ", " << num2;

}

and the content of test.txt is ...

Code:
34 54

when I run my program it will print out

Code:
34, 54

I hope my question makes sense and appreciate your help!

*EDIT* Yes, I have an xcode project of command line tool already created
 
Please no one stated, or implied, you as incompetent.

For testing purposes within Xcode where do you plan to keep the file "test.txt" in relation to your Xcode project.

If it is in the folder with the project file -

Select "Edit Active Executable <your project name>" from the "Project" menu in Xcode. A dialog titled "Executable <your project name> Info" will appear.

Select the "General" tab

Click the radio button "Project directory"

Select the "Arguments" tab

Click the "+" below the "Arguments" pane.

Into the activated edit field type "< test.txt" without the quotes.

Close the dialog

Recompile, test, debug, loop ...
 
Thanks again for your reply. I'm not the most experienced with Xcode, so I really appreciate your help.

I have placed "test.txt" into both the project directory and the build/debug directory. I also have "< test.txt" in my argument list; however, it does still not work for some reason. If I compile and run it myself within terminal, it works fine, but in Xcode it just waits for me to type in a value... any other ideas?
 
Thanks again for your reply. I'm not the most experienced with Xcode, so I really appreciate your help.

I have placed "test.txt" into both the project directory and the build/debug directory. I also have "< test.txt" in my argument list; however, it does still not work for some reason. If I compile and run it myself within terminal, it works fine, but in Xcode it just waits for me to type in a value... any other ideas?

A working project has been attached.

Well it appears these setting are insert on a per user basis and did't transfer with the archive.

You could still try adding the settings described above to this project and see if that works.

attachment.php


attachment.php


attachment.php
 

Attachments

  • ConsoleTool.zip
    3.8 KB · Views: 418
  • Screen shot 2009-09-15 at 11.21.47 AM.png
    Screen shot 2009-09-15 at 11.21.47 AM.png
    65.4 KB · Views: 4,098
  • Screen shot 2009-09-15 at 11.19.55 AM.png
    Screen shot 2009-09-15 at 11.19.55 AM.png
    73.4 KB · Views: 4,096
  • Screen shot 2009-09-15 at 11.19.47 AM.png
    Screen shot 2009-09-15 at 11.19.47 AM.png
    48.1 KB · Views: 4,050
Downloaded your attachment, followed the steps in your previous post, but still no luck :-( I'm running out of ideas as to how to get this working (likely you are as well) so I think I will just have to test on a Linux command line. Again, really appreciate your help!
 
Downloaded your attachment, followed the steps in your previous post, but still no luck :-( I'm running out of ideas as to how to get this working (likely you are as well) so I think I will just have to test on a Linux command line. Again, really appreciate your help!

You could change your C++ code so it uses a file input stream you open and close, rather than the stdin stream of cin.

You could also add a custom executable that runs /bin/bash and has arguments -c and "cd /x/y/z;yourTest <someFile".
 
The project requires us to use stdin for all input, so ifstream is not an option. I ended up just separately compiling in Terminal... which leads me to another question: Is there an easy way to get to a directory within Terminal? Looking for a way around typing "cd dir/dir1/dir2/dir3/..."

Thanks!
 
The project requires us to use stdin for all input, so ifstream is not an option. I ended up just separately compiling in Terminal... which leads me to another question: Is there an easy way to get to a directory within Terminal? Looking for a way around typing "cd dir/dir1/dir2/dir3/..."

Enter cd dir/dir1/dir2/dir3/... into a text file in TextEdit.app. Save it. In future, copy the line and paste it into a Terminal window.

You can also drag selections of multiple lines into a Terminal window. The commands will run in sequence.

Example 1

Code:
cd ~/Library/Preferences
ls -la *Xcode*
cd ~
echo Done.
Example 2

Code:
cd ~/Library/Preferences; ls -la *Xcode*
 
Downloaded your attachment, followed the steps in your previous post, but still no luck :-( I'm running out of ideas as to how to get this working (likely you are as well) so I think I will just have to test on a Linux command line. Again, really appreciate your help!

Alex,

I'm not one for giving up easily so I hope you won't mind if I continue to poke at this problem.

Would you mind archiving and attaching your problem project. I'd like to take a look at it and see if we can get it to work.
 
Here it is. Thanks for your persistence! BTW, please don't hate on my lack of coding expertise :)
 

Attachments

  • Project 1 - Kirby's Maze.zip
    515.7 KB · Views: 390
Here it is. Thanks for your persistence! BTW, please don't hate on my lack of coding expertise :)

Here is the corrected project file.

You somehow missed setting your projects "current" or "working" directory to that of the project as instructed (see second image) above and reiterated here:

  1. Select "Edit Active Executable <your project name>" from the "Project" menu in Xcode. A dialog titled "Executable <your project name> Info" will appear.
  2. Select the "General" tab
  3. Click the radio button "Project directory"
 

Attachments

  • Project 1 - Kirby's Maze.xcodeproj.zip
    9.4 KB · Views: 189
That does not fix the error. I put a "test.txt" in both the project and build/debug directory, neither or which work.
 
Alex,

I'm using Xcode 3.2 which means I'm on Mac OS X - 10.6 or SnowLeopard, where it works properly for me.

I installed Mac OS X - 10.5.8, Xcode 3.1.4 using the default Xcode preference settings where it also failed for me.

I noted the Debugger and Console windows didn't open automatically which is what I like to have happen during all my development. So I went into Xcode "Debugging" Preferences and set "On Start:" to "Show Console and Debugger" and ran the test again at which point it worked for me.

Make sure you've set all the previous discussed execution settings.

Let us know if setting your Xcode "Debugging" Preferences in this way works for you.
 
You are my hero, Lloyd!! That worked perfectly. Thank you so much for your help.
 
Great,

Redirecting executable I/O need only be done from the debugging environment. To me that seemed sensible from my first experiences with Xcode. Sorry it took me so long to realize others may not have made the same assumption (whether right or wrong on my part).

Now yo can get back to enjoying your CS assignments!!!
 
Yeah, I would've never guessed. Now to actually get this assignment done :p Thanks again!
 
anyone got it working for xcode 4.5? i'm currently in the same situation the creator of this thread was, unfortunately it seems like the menus etc. changed name and/or location, any help would be greatly appreciated!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.