开发者

Read french characters in text file

开发者 https://www.devze.com 2023-03-03 10:05 出处:网络
Hi can\'t read the french accent (like \'é\') in a text file, this is the piece of code I use to open and read the file...

Hi can't read the french accent (like 'é') in a text file, this is the piece of code I use to open and read the file...

FILE* file = fopen( [MyFileName UTF8String], "r, ccs=UTF-8");
if (file != 0)
{
    while(fgets(buffer, 1024, file) != NULL)
    {      
        NSString* string = [[NSString alloc] initWithCString: buffer];
        //Do many things.....
    }
    fclose(file);
}

I can't see where is the 开发者_运维知识库problem, my file is correctly encoded in UTF8...

Thanks!


The fgets function in c is ASCII only, you need to use the fgetws. This is the wide version of fgets for use with multibyte encodings.


Instead of fopen and fgets you can read the content of file in a NSString by using stringWithContentsOfFile:usedEncoding:error: method directly.

NSString *fileContent = [NSString stringWithContentsOfFile:myFilePath usedEncoding:NSUTF8StringEncoding error:NULL];
0

精彩评论

暂无评论...
验证码 换一张
取 消