Xcode c++ - Input files on command line

Discussion in 'Mac Programming' started by alexWoz, Sep 14, 2009.

  1. alexWoz macrumors newbie

    Joined:
    Sep 12, 2009
    #1
    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?
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Redacted - Don't feel well thus it wasn't a coherent reply
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    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.
     
  4. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #4
    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
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    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 ...
     
  6. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #6
    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?
     
  7. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    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.

    [​IMG]

    [​IMG]

    [​IMG]
     

    Attached Files:

  8. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #8
    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!
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    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".
     
  10. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #10
    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!
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    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*
    
     
  12. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #12
    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.
     
  13. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #13
    Here it is. Thanks for your persistence! BTW, please don't hate on my lack of coding expertise :)
     

    Attached Files:

  14. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #14
    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"
     

    Attached Files:

  15. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #15
    That does not fix the error. I put a "test.txt" in both the project and build/debug directory, neither or which work.
     
  16. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #16
    That's odd!

    What versions of Mac OS and Xcode are you working with?
     
  17. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #17
    Very odd :-( 10.5.8 / 3.1.4
     
  18. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #18
    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.
     
  19. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #19
    No luck with that either. This is quite frustrating!
     
  20. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #20
    Could you describe how you run the compiled code from within Xcode?
     
  21. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #21
    Try running running using Command-Y, Debug mode!
     
  22. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #22
    You are my hero, Lloyd!! That worked perfectly. Thank you so much for your help.
     
  23. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #23
    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!!!
     
  24. alexWoz thread starter macrumors newbie

    Joined:
    Sep 12, 2009
    #24
    Yeah, I would've never guessed. Now to actually get this assignment done :p Thanks again!
     
  25. niklot macrumors member

    Joined:
    Oct 13, 2008
    #25
    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!
     

Share This Page