Passing NSString to system()

  • I am trying to insert the contents of a NSString into a c-string, to
    be used in a system() call.  I am having problems.

    I've tried:

    system("%", myString);

    without success.  I know I must be missing something.

    Thank you
    AA
  • You're missing lots of things, unfortunately.

    Firstly, the specifier for C strings is %s, not %.

    Secondly, system() does not take a format string, nor is it a variable
    argument list function like printf, etc. You'd have to use something
    like snprintf to construct a string in memory before handing it off to
    system().

    Thirdly, you convert NSStrings to C strings by calling, for example,
    [myString UTF8String].

    Fourthly, there's an Objective-C class for executing commands which is
    far more powerful than system() that you probably should be using
    instead (and also doesn't necessarily have the overhead of invoking a
    new shell). Read about NSTask in the manual.

    -- Finlay

    On 04/10/06, Aychamo Aychamo <aychamo...> wrote:
    > I am trying to insert the contents of a NSString into a c-string, to
    > be used in a system() call.  I am having problems.
    >
    > I've tried:
    >
    > system("%", myString);
    >
    > without success.  I know I must be missing something.
    >
    > Thank you
    > AA
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<finlay.dobbie...>
    >
    > This email sent to <finlay.dobbie...>
    >
  • On Oct 4, 2006, at 12:43 PM, Aychamo Aychamo wrote:

    > I am trying to insert the contents of a NSString into a c-string, to
    > be used in a system() call.  I am having problems.
    >
    > I've tried:
    >
    > system("%", myString);
    >
    > without success.  I know I must be missing something.
    >
    > Thank you
    > AA

    Documentation is your friend!  Have you read through the NSString
    page? In particular, be on the lookout for: cStringUsingEncoding

    pete
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today