Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading String from File with Objective C

This one is weird. Hopefully I will ask the right question:

I'm using an md5 method to create a checksum value which I then write to a file. Then afterwards I read the file using this:

NSString * id_From_File = [[NSString alloc]
                                  initWithContentsOfFile:path_to_ID
                                  encoding:NSUTF8StringEncoding
                                  error:&error];

The result gets placed in a NSString which when I print gives me very strange behaviour. For example when I use this to print,

id_with_date = [NSString stringWithFormat:@"  %@  %@", dateString, id_From_File];

it will print both strings if dateString is placed in the first parameter and id_From_File in the second. If I switch them around (which I need to do) only id_From_File shows.

Edit 1: Example of the switch:

id_with_date = [NSString stringWithFormat:@" %@  %@", id_From_File, dateString];

I strongly believe this has something to do with the encoding of the id_From_File string.

Any knowledge!?

Thanks,

like image 786
Eric Brotto Avatar asked Jun 26 '26 00:06

Eric Brotto


1 Answers

NSString should actually be capable of recognizing null characters as the file ending. Did you try to use a different method to load the string. I'd go for this one instead:

- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

This method automatically detects the file's encoding instead of decoding it with a fixed one.

like image 190
Max Seelemann Avatar answered Jun 27 '26 16:06

Max Seelemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!