0

I have a project in which I need to read a .txt file automatically through program arguments. I have tried typing "< input.txt" into the arguments tab but it does not seem to be reading it. I wrote a simple code to test this quicker and that still doesn't work. If anyone else knows how to solve this problem that would be great, thanks.

#include <iostream>

using namespace std;

int main() {
    int r, c;
    cin >> r >> c;
    cout << r << " " << c << endl;
    return 0;
}

photo of directory [

snipshow7
  • 87
  • 1
  • 8

1 Answers1

1

It is enough complected task in Xcode 9. Suppose input.txt is the file that we need to pass to the standard input of our program like prog < input.txt.

The definitions of Xcode build environment variables are explained in How do I print a list of "Build Settings" in Xcode project?

  1. Open your project Build Phases. Press + there to add new Run Script task. Enter the command bellow. It expects input.txt is placed in the same directory where the program executable is built, see the value of ${BUILT_PRODUCTS_DIR} in the build log.

    echo "${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME} < ${BUILT_PRODUCTS_DIR}/input.txt" > ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}.sh
    

enter image description here 2. Add new scheme and edit it. For the Run task goto Info pane. Select Terminal.app in the Executable and uncheck Debug executable.

enter image description here

  1. Goto Arguments pane. Press + and enter the command into the new argument

    ${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}.sh
    

enter image description here

  1. Finally build and run your application. New Terminal app must start and you must see like below there (it is from your code snipped)

enter image description here

273K
  • 29,503
  • 10
  • 41
  • 64
  • So I can only do this from running xcode through the command line tool? – snipshow7 Feb 03 '18 at 04:54
  • Hi thanks for the help, but I followed all of these steps and the terminal opens on running but it doesn't display anything like yours. – snipshow7 Feb 03 '18 at 08:33
  • What does it output? – 273K Feb 03 '18 at 08:49
  • It just says last login and my mabook name – snipshow7 Feb 03 '18 at 09:22
  • You did something wrongly. Try to repeat all steps carefully. – 273K Feb 03 '18 at 09:24
  • Im just supposed to copy and paste exactly what you said correct? – snipshow7 Feb 03 '18 at 17:56
  • I have followed these steps 100% and when I click to run the code all I get in terminal is...Last login: Sat Feb 3 11:59:40 on ttys347 MacBook:~ Mac$ – snipshow7 Feb 03 '18 at 18:02
  • Mostly yes, copy and paste is correct. The name of your input file supposed to be `input.txt` and the file must be placed in the same directory, there the executable is built. – 273K Feb 03 '18 at 18:27
  • yes i believe I did that, I added a photo, is that what you mean? Also it is input1.txt and i changed the name accordingly in your run script code/ – snipshow7 Feb 03 '18 at 18:51
  • Do not paste links to photos, it is prohibited here, your question will be downvoted. Please read my comments carefully, try to understand my pasted commands. `main.cpp` is not your compiled executable file in the built products directory, it is the source file in the project directory. Thus you placed `input1.txt` into a wrong place. Update the command in the **Run Script** or move your `input1.txt` into the correct place. I am going to stop further help here after this comment. – 273K Feb 04 '18 at 03:30
  • It didn't let me paste the photo, thats the only way I could show it, sorry. I moved the input.txt into the products folder but still no fix. – snipshow7 Feb 04 '18 at 04:58
  • Compare the value of `${BUILT_PRODUCTS_DIR}` in the build log with the base path of your `input1.txt`, your products dir. Or replace `${BUILT_PRODUCTS_DIR}/input.txt` with the desired full path of `input1.txt` – 273K Feb 04 '18 at 08:08