Running C scripts with terminal instead of Xcode

2019-02-15 20:22发布

问题:

Currently I am developing a couple of C programs on my mac using Xcode. There however is 1 problem. My study requires me to use some sort of input field through the coding. So for instance if the users wants to run the program 10 times or wants the program to create 10 answers. I am using "atoi(argv[1])" just to get the input from the user.

This is exactly the problem. As soon as I run the program it just starts to bug, which is normal I quess because he is waiting for the input and not receiving it or something else. Anyways I tried to solve this problem with this link: How to run command-line application from the terminal?

This unfortunately didnt solve it either. I have already tried to re-install xcode, because just entering gcc in my terminal doesnt work either, but everytime the app store auto instals it for me.

Does anyone has a fix for my problem. I would greatly appreciate it due to the fact that next friday I have another deadline :( and I wont be getting a sufficient grade if my user input is not working.

Your help is again much appreciated!

Greetings,

Kipt Scriddy

EDIT: To clarify the problem. When running the script I want it to pop up in Terminal and wether there is an input field reguired it should ask for input. At the moment he is crashing immediatly due to the lack of input. He is not waiting for the argument to be passed from the users. He is skipping that part

回答1:

HOWTO Pass command line arguments to your program from Xcode IDE

  1. Open your project's active Scheme. Easiest way to do this is from the main menu. Select Product/Scheme/Edit Scheme...
  2. In the schema editor, you'll see several build targets on the left side; the one you want is the "Run YourProjectName" target. Select it.
  3. On the right side you'll see four sub-tabs, including Info, Arguments, Options, and Diagnostics. Select Arguments
  4. Add/Remove any arguments you need. In your case, add /phi as the first argument, then 10 as the second.

Noteworthy: This is also where you can specify the current working directory of your program at launch rather than the long, temp path Xcode uses when building your binaries. To do so:

  1. Perform steps 1-2 from above.
  2. Select the Options sub-tab
  3. Click the "Use custom working directory" checkbox.
  4. Specify the full path where you want Xcode to execute your program from.

That in combination with getting your parameter handling fixed in your program should get you up and running.



回答2:

Sounds to me like you want to get your arguments from the command line and if they are missing, prompt the user for them

Lets assume you want the arguments: number word number

#include <stdio.h>
#include <string.h>

int number1;
char word[128];
int number2;

int main(int argc, const char **argv)
{
    if (argc == 4)
    {
        number1 = atoi(argv[1]);
        strcpy(word, argv[2]);
        number2 = atoi(argv[3]);
    }
    else
    {
        do
            printf("Enter number word number: ");
        while (scanf("%d %s %d", &number1, word, &number2) != 3);
    }

    printf("I got %d '%s' %d\n", number1, word, number2);
    return 0;
}

Which gives:

$ ./test
Enter number word number: 1 andy 12
I got 1 'andy' 12
$ ./test 2 pandy 56
I got 2 'pandy' 56

Note that the error-checking is poor in this example and can be improved alot (not using atoi() is one way to start).



回答3:

It sounds like you need to check argc in the program as RageD points out, otherwise when launching the program with insufficient arguments will cause problems.

gcc is the c compiler - it produces an executable. When you hit 'Run' in Xcode it compiles your program and then runs the executable file created. The executable created is named the same as your Xcode project name.

To run the program you built in Xcode from the command line:

  • In Xcode's project navigator find the executable in the 'Products' folder
  • Drag the executable file into Terminal (You will get an absolute url to the executable)
  • add any arguments you need to run your program
  • Hit enter!

The result will look something similar to the snippet below (for my 'MyCommandLineApp' project):

$ /Users/pliskin/Library/Developer/Xcode/DerivedData/MyCommandLineApp-hbpuxhguakaagvdlpdmqczucadim/Build/Products/Debug/MyCommandLineApp argument1 argument2