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: Gonzalo Castro <email@hidden>
- Date: Mon, 27 Aug 2007 16:26:33 -0400
I got the answer off-list. I needed to add the following after -
launch so execution waits until the launch is finished.
[myTask waitUntilExit];
Thanks Dennis!
Gonzalo
On Aug 27, 2007, at 3:43 PM, Gonzalo Castro wrote:
Why does the following log an empty string for the fileContents?
NSString *path = @"/tmp/myFile.tmp";
if ( [[NSFileManager defaultManager] fileExistsAtPath: path] )
{
[[NSFileManager defaultManager] removeFileAtPath: path
handler: nil];
}
BOOL fileCreated = [[NSFileManager defaultManager]
createFileAtPath: path contents: @"" attributes: nil];
if ( fileCreated )
{
NSFileHandle *fileHandle = [NSFileHandle
fileHandleForWritingAtPath: path];
NSTask *myTask = [[NSTask alloc] init];
[myTask setLaunchPath: @"/bin/ps"];
[myTask setArguments: [NSArray arrayWithObject: @"axwww"]];
[myTask setStandardOutput: fileHandle];
[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?
Thanks,
Gonzalo
_______________________________________________
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