8

I have a nsstring(filePath), which has the path to the audio file. I want open the audio file, so I want to convert the nsstring to Cstring.

  fopen([filePath cStringUsingEncoding:1], "r");

is the above line is correct or not, because I can also use fopen([filePath cString], "r");. In some websites it is mentioned to use UTF8stringEncoding. Which is the the right NSString to string conversion?

halfer
  • 19,824
  • 17
  • 99
  • 186
Warrior
  • 39,156
  • 44
  • 139
  • 214
  • Pro-tip for English writing: punctuation marks should have a space after them, in order to separate sentences and phrases out from each other. This also allows a more natural form of line-wrapping, so that (for example, from your text) the string `not,because` is not treated by web clients as one word. – halfer Apr 01 '18 at 12:10

2 Answers2

17

Use UTF8String:

fopen([filePath UTF8String], "r");
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
6

Don't use UTF8String, but fileSystemRepresentation.

You should do this because HFS+ or other file systems are not strictly compatible with the Unicode system. Another reason: This method operates by replacing the abstract path and extension separator characters (‘/’ and ‘.’ respectively) with their equivalents for the current file system.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Antoine Rosset
  • 1,045
  • 2
  • 13
  • 22