As an example, I want to write out some string value, str
, to a file, "yy"
, as it changes through each iteration of a loop. Below is how I'm currently implementing it:
NSOutputStream *oStream = [[NSOutputStream alloc] initToFileAtPath:@"yy" append:NO];
[oStream open];
while ( ... )
{
NSString *str = /* has already been set up */
NSData *strData = [str dataUsingEncoding:NSUTF8StringEncoding];
[oStream write:r(uint8_t *)[strData bytes] maxLength:[strData length]];
...
}
[oStream close];
Ignoring the UTF-8 encoding, which I do require, is this how NSString
s are typically written to a file? That is, by first converting to an NSData
object, then using bytes
and casting the result to uint8_t
for NSOutputStream
's write
?
Are there alternatives that are used more often?
EDIT: Multiple NSString
s need to be appended to the same file, hence the loop above.