Read french characters in text 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];
精彩评论