Showing posts with label fileHandleWithStandardOutput. Show all posts
Showing posts with label fileHandleWithStandardOutput. Show all posts

Saturday, April 27, 2013

NSFileHandle fileHandleWithStandardOutput example ios


fileHandleWithStandardOutput

Returns the file handle associated with the standard output file.
+ (id)fileHandleWithStandardOutput
Return Value
The shared file handle associated with the standard output file.
Discussion of [NSFileHandle fileHandleWithStandardOutput]
Conventionally this is a terminal device that receives a stream of data from a program. There is one standard output file handle per process; it is a shared instance.[NSFileHandle fileHandleWithStandardOutput]
When using this method to create a file handle object, the file handle owns its associated file descriptor and is responsible for closing it.
Example of [NSFileHandle fileHandleWithStandardOutput]
// Create a file handle for reading an arbitrary file
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:p1];
fh = [NSFileHandle fileHandleForReadingAtPath:@"/dev/srandom"];

// Create a file handle for writing to a file
fh = [NSFileHandle fileHandleForWritingAtPath:p2];
fh = [NSFileHandle fileHandleForWritingAtPath:@"/dev/null"];

// Create commonly used file handles
fh = [NSFileHandle fileHandleWithStandardError];
fh = [NSFileHandle fileHandleWithStandardInput];
fh = [NSFileHanfle fileHandleWithStandardOutput];
fh = [NSFileHandle fileHandleWithNullDevice];

// Write data to a file handle
NSString *str = @"Other Unix boxes";
NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding];
[fh writeData:data];

// Read data from a file handle;
// this could be used for something like % echo "Hello" | ThisApp
fh = [NSFileHandle fileHandleWithStandardInput];

// Read all available data and converting it to a string
NSData *data = [fh availableData];
NSString *str = [NSString stringWithData:data];

// You can also read a specified number of bytes
data = [fh readDataOfLength:400];

// Or you can read up to an end-of-file
data = [fh readDataToEndOfFile];

// And close it when finished
[fh closeFile];