Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

msharp

macrumors regular
Original poster
Hi there,

I've been really struggling against a very simple (seems to be) issue in the past 48 hours, so I guess I should ask for some tips here.

Here's the thing: I wanted to read a file using utf-8 encoding, and save the file content as a NSString. Here's my code:
Code:
DBPath = [NSString stringWithUTF8String:[[NSData dataWithContentsOfFile:filePath] bytes]];

Well, it can perfectly run without any warnings/errors, BUT it always produces a few more characters to the file content in the NSString.

For example, the file content may be: "/Users/myname/Document.doc", but the NSString seems to be "/Users/myname/Document.docame/Library/P" or something like that.

Some times, the NSString is simply "/Users/myname/Document.doc?" (with a question mark added to the end of the string).

So, could anyone give me some hint for this issue?

Thanks a lot in advance!
 
Try it like this
Code:
NSString *DBString = [NSString stringWithContentsOfFile: filePath
					       encoding: NSUTF8StringEncoding
						  error: nil];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.