Running C scripts with terminal instead of Xcode

前端 未结 3 591
时光取名叫无心
时光取名叫无心 2021-01-07 11:50

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.

相关标签:
3条回答
  • 2021-01-07 12:01

    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).

    0 讨论(0)
  • 2021-01-07 12:25

    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
    
    0 讨论(0)
  • 2021-01-07 12:26

    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.

    0 讨论(0)
提交回复
热议问题