• 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
NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
      • From: Gonzalo Castro <email@hidden>
    • Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
      • From: Randall Meadows <email@hidden>
  • Prev by Date: RS: going from the name of a method to the method itself
  • Next by Date: Re: RS: going from the name of a method to the method itself
  • Previous by thread: Re: RS: going from the name of a method to the method itself
  • Next by thread: Re: NSTask setStandardOutput: then stringWithContentsOfFile gives empty string
  • Index(es):
    • Date
    • Thread