Reputation: 35
Im trying to read a .csv file in iOS, when I use NSUTF8StringEncoding, it prints null - when I use NSUTF16StringEncoding, it prints my file as chinese characters.. Can anyone inform me as to whats going on here? I have dragged my .csv into the main project directory, and coppied files. Here is my sample code:
NSError *error;
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Crane_Data" ofType:@"csv"];
NSString *fileData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"TEST");
NSLog(@"%@",fileData);
NSLog(@"TEST");
After error test using:
if (!fileData) NSLog(@"%@", error);
I got this with NSUTF8
2014-06-20 22:20:57.351 CollectionView[6084:390557] Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0xaf5a7b0 {NSFilePath=/Users/Dylan/Library/Developer/CoreSimulator/Devices/D3F54177-0F08-47A5-9FE0-785E2AF0691A/data/Containers/Bundle/Application/81942FA8-BB67-4E8D-826E-7D1A445DD9B0/CollectionView.app/Crane_Data.csv, NSStringEncoding=4}
And no error printed on the console using NSUTF16
Upvotes: 0
Views: 280
Reputation: 112855
The encoding is plane ASCII:
I took the bytes from the comments and formatted them for a char string, then created a NSString:
char* bytes = "\x66\x6f\x72\x74\x75\x6e\x65\x5f\x69\x64\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x61\x63\x74\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x74\x79\x70\x65\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x72\x6f\x6f\x74\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x73\x74\x72\x69\x6e\x67\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x64\x65\x73\x63\x72\x69\x70\x74\x69\x6f\x6e\x0d\x31\x2c\x31\x2c\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x2c\x70\x2c\x4d\x61\x6b\x65\x20\x61\x6e\x20\x6f\x72\x69\x67\x61\x6d\x69\x20\x63\x72\x61\x6e\x65\x20\x2c\x0d\x32\x2c\x31\x2c\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x2c\x70\x2c\x57\x72\x69\x74\x65\x20\x6f\x6e\x65\x20\x6c\x69\x6e\x65\x20\x6f\x66\x20\x73\x6f\x6d\x65\x74\x68\x69\x6e\x67\x2e\x2c\x54\x68\x69\x73\x20\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x20\x63\x68\x61\x6c\x6c\x65\x6e\x67\x65\x20\x69\x73\x20\x69\x6e\x74\x65\x6e\x64\x65\x64\x20\x74\x6f\x20\x62\x65\x20\x63\x72\x65\x61\x74\x69\x76\x65\x2e\x20\x49\x74\x20\x63\x61\x6e\x6e\x6f\x74\x20\x62\x65\x20\x61\x20\x6c\x00";
NSUInteger bytesLength = strlen(bytes);
NSString *string = [[NSString alloc] initWithBytes:bytes length:bytesLength encoding:NSASCIIStringEncoding];
NSLog(@"string: %@", string);
NSLog Output:
string: fortune_id,fortune_act,fortune_type,fortune_root,fortune_string,fortune_description
1,1,composition,p,Make an origami crane ,
2,1,composition,p,Write one line of something.,This composition challenge is intended to be creative. It cannot be a l
Below is the initial response:
If it is utf-16 there are three possibilities:
NSUTF16StringEncoding
NSUTF16BigEndianStringEncoding
NSUTF16LittleEndianStringEncoding.
Try all three.
If that fails get the data:
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
Then log it:
NSLog(@"fileData: %@", fileData);
Post the first hundred bytes of so. It should be easy to figure out the format.
Also post what it should be if you have that information.
Upvotes: 1