When I’m saving a simple text file from my iPhone app as NSUTF16StringEncoding, it seems to like adding an extra ‘space’ at the start of each new line that I add. This is some rough code detailing what I’m doing (not exact, and not necessarily executable as is).
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *tempPath = [paths objectAtIndex:0];
NSString *tempFilename = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
NSString *tempFilePath = [tempPath stringByAppendingPathComponent:tempFilename];
NSError *error;
freopen([tempFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:tempFilePath];
NSString *testLine1 = @"test row 1";
[fileHandle seekToEndOfFile];
[fileHandle writeData:[testLine1 dataUsingEncoding:NSUTF16StringEncoding]]; // NSUTF8StringEncoding
NSString *testLine2 = @"test row 2";
[fileHandle seekToEndOfFile];
[fileHandle writeData:[testLine2 dataUsingEncoding:NSUTF16StringEncoding]]; // NSUTF8StringEncoding
NSString *testLine3 = @"test row 3";
[fileHandle seekToEndOfFile];
[fileHandle writeData:[testLine3 dataUsingEncoding:NSUTF16StringEncoding]]; // NSUTF8StringEncoding
[fileHandle closeFile];
// convert csv text to data
NSData *dataCsvOutput = [NSData dataWithContentsOfFile:tempFilePath];
NSArray *objects = [NSArray arrayWithObjects:dataCsvOutput, exportFilename, deviceType, nil];
NSArray *keys = [NSArray arrayWithObjects:@"data", @"filename", @"devicetype", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
NSString *exportFilename = [withDictionary objectForKey:@"filename"];
NSData *dataCsvOutput = [withDictionary objectForKey:@"data"];
NSString *deviceType = [withDictionary objectForKey:@"devicetype"];
MFMailComposeViewController *mfViewController = [[MFMailComposeViewController alloc] init];
mfViewController.mailComposeDelegate = self;
[mfViewController setSubject:@"Test"];
[mfViewController setMessageBody:[NSString stringWithFormat:@"Please find data attached."] isHTML:NO];
[mfViewController addAttachmentData:dataCsvOutput mimeType:@"text/plain;charset=utf-16" fileName:exportFilename];
[self presentModalViewController:mfViewController animated:YES];
[mfViewController release];
The resulting text file ends up being something like:
test row 1
test row 2
test row 3
Can anyone suggest why that is ? It works fine when exported as UTF-8.
Thanks.
NSUTF16StringEncoding(which is an alias toNSUnicodeStringEncoding) prepends the output with a so-called “byte-order marker”, which consists of the bytes “FE FF” or “FF FE” and indicates whether the following data is big-endian or little-endian UTF-16.To avoid the byte-order marker, use either
NSUTF16BigEndianStringEncodingorNSUTF16LittleEndianStringEncoding.Remark: A better alternative for
is