Write chinese text on txt file

Hello everybody,

I have the following problem in my iOS application.

My application allows a user to write some text and save it in a txt file, but this doesn’t work for a chinese user.

A chinese user is able to write chinese text on the device and the text appears correctly on the device but it is saved incorrectly on the text file. If the user load the saved file the text is loaded with incorrect characters.

To save file I use the following instructions in Objective C:

[fileToSave writeToFile:path atomically:YES encoding:NSISOLatin1StringEncoding error:&error]

I suppose the encoding is not right but what encoding do I have to use to be compatible with all languages?

Many, many thanks in advance for your help!

Regards,
Giordano

Try encoding with NSUTF8StringEncoding.

Change encoding by replacing NSISOLatin1StringEncoding with NSUTF8StringEncoding

Again when you read the file use NSUTF8StringEncoding the same.

*NSString str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

Best

Yes, I tested and it seems to work! Very simple!

The problem is that I used NSISOLatin1StringEncoding because I had a problem that I don’t remind now.

Do you think NSUTF8StringEncoding is good for all text characters?

Thanks and regards
Giordano

Most probably it will work fine with NSUTF8StringEncoding.

Sometimes it depends when you use WEB APIs the documentation for the WEB APIs probably says what encoding is used. When you fetching Web API data you should use whatever encoding the service is using. You can discuss with API developer for the same or you can try out with the more different languages.

In most cases NSUTF8StringEncoding will work fine.

Hi,

do you remind this post, I was not able to read chinese character and you suggested me to use NSUTF8StringEncoding. I answered you that now works well but today I found that with NSUTF8StringEncoding I can’t read large file. If I try to read a file of about 9MB I have the following error:

Error Domain=NSCocoaErrorDomain Code=261 “The file “KingBaseLite.txt” couldn’t be opened using text encoding Unicode (UTF-8).” UserInfo={NSFilePath=/Users/giordanovicoli/Library/Developer/CoreSimulator/Devices/8BEEEF25-620C-4B29-BD4A-E31B3C169E05/data/Containers/Data/Application/D0394678-DCCC-4D3E-8F9F-3B8685C80625/Documents/KingBaseLite.txt, NSStringEncoding=4}

If I change the Encoding with NSISOLatin1StringEncoding the file is read correctly but I am not able to save chinese text. Is there a solution that satisfies all requests?

Many thanks and regards,
Giordano

What error/result you’re having when you try to save the chinese text?

Or try using NSISOLatin2StringEncoding

Hi,

thank you very much for the fast answer.

Using NSISOLatin2StringEncoding I can read large file but I can’t save chinese text:

Error Domain=NSCocoaErrorDomain Code=517 “The file couldn’t be saved using the specified text encoding.” UserInfo={NSFilePath=/Users/giordanovicoli/Library/Developer/CoreSimulator/Devices/C5E8E0D3-0E45-45E2-9931-6AB4A7988B3F/data/Containers/Data/Application/ACAB9507-7AF4-4C0C-B118-D0BAC0CEA37F/Documents/pippo.pgn, NSStringEncoding=9}

It is a very strange problem!

Regards,
Giordano

Try it once.

  1. Write using NSUTF8StringEncoding.
  2. When you read the file do the same what you are doing right now (Using NSISOLatin2StringEncoding).
1 Like

In this way it saves correctly chinese text but when I read it reads other characters.

If I try to read previous database with Chinese text it crashes.

Giordano

Okay…

I will get back to you with some more research for the same.

Ok, many thanks.

Giordano