Hi, all.

I tried to set the offset property of a file NSOutputStream by 
NSStreamFileCurrentOffsetKey with appending enabled with code listed below. But 
it doesn't work.

The output is "1234567890abcdefghij!@#$%^&*()ABCDEFGHIJ" as s1 plus s2 if you 
remove output.txt before running this demo program. The setProperty:forKey 
returns NO and querying of the NSStreamFileCurrentOffsetKey property returns 
nil. I've tried the Core Foundation counterpart CFWriteStreamSetProperty with 
kCFStreamPropertyFileCurrentOffset, but no magic happens.

If appending is disabled, everything is fine. But I don't want to replace the 
original file. The document says that NSStreamFileCurrentOffsetKey allows me to 
manipulate the current read or write position in file-based streams. So I 
wonder why this operation failed.


NSString *s1 = @"1234567890abcdefghij";
NSString *s2 = @"!@#$%^&*()ABCDEFGHIJ";
char *cwd = getcwd(NULL, 0);
NSString *currentWD = [NSString stringWithCString:cwd 
encoding:NSASCIIStringEncoding];
free(cwd);

NSOutputStream *outStream = [NSOutputStream outputStreamToFileAtPath:[currentWD 
stringByAppendingPathComponent:@"output.txt"] append:YES];
[outStream open];
[outStream write:(const uint8_t *)[s1 UTF8String] maxLength:[s1 length]];
NSLog(@"Current file offset: %@", [outStream 
propertyForKey:NSStreamFileCurrentOffsetKey]);
BOOL result = [outStream setProperty:@(0) forKey:NSStreamFileCurrentOffsetKey];
NSLog(@"result is %d, %@", result, [outStream streamError]);
NSLog(@"Current file offset: %@", [outStream 
propertyForKey:NSStreamFileCurrentOffsetKey]);
[outStream write:(const uint8_t *)[s2 UTF8String] maxLength:[s2 length]];
[outStream close];


Thanks,
Hank

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to