• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Using an NSString as standard input to an NSTask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using an NSString as standard input to an NSTask


  • Subject: Re: Using an NSString as standard input to an NSTask
  • From: Aki Inoue <email@hidden>
  • Date: Wed, 26 Oct 2005 11:11:36 -0700

Andrei,

Since cStringLength depends on the default encoding (not UTF-8), the use of the method along with UTF8String causes issues.

Use -[NSString dataUsingEncoding:] instead.

Aki

Try something like this
...
NSTask* task = [[ NSTask alloc ] init ];
... normal task setup code...
[ task setStandardInput: [[ NSPipe alloc ] init ]];
[ task launch ];
NSFileHandle* taskInput = [[ task standardInput ] fileHandleForWriting ];


const char* cStringToSendToTask = [ stingToSendToTask UTF8String ];
int cStringToSendToTaskLength = [ stingToSendToTask cStringLength  ];

[ taskInput writeData: [ NSData dataWithBytes: cStringToSendToTask length: cStringToSendToTaskLength ]];
...


On Oct 26, 2005, at 9:41 AM, Ken Tabb wrote:

Hi folks,

is there an easy way to supply an NSString as the standard input for an NSTask? I've looked at NSTask, NSString (and associated), NSFileHandle and NSPipe but can't see an obvious way to do it.

I'm currently writing temporary files, firing off an NSTask using the temporary file as its standard input, and deleting the temporary file. This works fine, but I will typically need to fire off the NSTask using a couple of thousand files each time (in a for loop, new NSTask & file per iteration), it means potentially abusing the user's disk space. Not least it must be quicker supplying an in-memory NSString than it is to write a file, and then load that file in the NSTask.

I guess I'm after the programmatic equivalent of:

myCLIApp < @"a string"

... using NSTasks and NSStrings

Carrying on from this, if it is possible to use in-memory NSStrings as input to NSTasks, what's the deal with memory management? i.e. would the NSTask retain the string, in which case I can release the string the moment I call the NSTask (having given it the string to look after), or would I have to wait until the end of the for loop before freeing up all the strings? I ask because the strings can be quite big, and there are a few thousand of them, so might have a VM / paging impact on the end user's app performance.

Any suggestions welcome,
Ken

- - - - - - - - - -
Dr. Ken Tabb
Mac & UNIX Developer - Health & Human Sciences
Machine Vision & Neural Network researcher - School of Computer Science
University of Hertfordshire, UK
http://www.health.herts.ac.uk/ken/


Certified non-Microsoft Solution Provider

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Using an NSString as standard input to an NSTask (From: Ken Tabb <email@hidden>)
 >Re: Using an NSString as standard input to an NSTask (From: Andrei Tchijov <email@hidden>)

  • Prev by Date: Re: Newbie Question: Find spaces inside a string
  • Next by Date: Re: problem unit testing core data
  • Previous by thread: Re: Using an NSString as standard input to an NSTask
  • Next by thread: Re: Cell Draw
  • Index(es):
    • Date
    • Thread