Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
- Subject: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
- From: Randall Meadows <email@hidden>
- Date: Mon, 27 Aug 2007 14:20:16 -0600
At 3:43 PM -0400 8/27/07, Gonzalo Castro wrote:
[myTask launch];
NSString *fileContents = [NSString stringWithContentsOfFile:
path encoding: NSUTF8StringEncoding error: nil];
NSLog(@" fileContents = \"%@\"", fileContents);
[myTask autorelease];
}
If I put a breakpoint at the top and step trough this code I do get
the file contents in the NSString. However, if I run without
breakpoints I get an empty string yet when I inspect /tmp/myFile.tmp
via the Terminal, it has the expected content.
Is there some file system flushing I need to do?
My guess is, the task has not finished by the time the next line has
executed. By breaking in the debugger, and via Terminal inspection,
the task has finished and the output available. You can probably
test this by putting a sleep() call after launch, but I'm not sure
the best way to correctly handle the situation.
Actually, yes, I do (after looking up NSTask in the dev docs). After
the [myTask launch], put a [myTask waitUntilExit] call; this will
ensure the task has completed and any output you're expecting will be
available.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden