NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
- Subject: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
- From: Gonzalo Castro <email@hidden>
- Date: Mon, 27 Aug 2007 15:43:28 -0400
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