1.txt 文件内容: 你好a,我是千叶!
期望结果: 你好a
C
#include <stdio.h> main() { FILE *fp; fp=fopen("1.txt","r"); char x[1000]; fread(x,sizeof(char),7,fp); //length=7,对于现在的1.txt结果正确,如果1.txt变成纯中文的文件,第三个汉字就会被截断,请问要怎么处理呢? printf("%s",x); }
====================================================================================
我的场景是文件比较大,不太想把整个文件读取到NSData或者NSString,所以希望NSData读取部分数据,再转化成NSString,于是就遇到了中文字符截取出现问题的情况。看了大家的回答,发现这个问题可能是个伪命题,毕竟文件的偏移是按字节算的不会去考虑文件字符编码。
之前提了一个问题在Object-c节点,没有人回答 所以想看看用C能不能解决,原问题:http://segmentfault.com/q/1010000002530834?_ea=128095
Give me an idea:
initWithData:encoding:
, and NSData has an initialization methoddataWithContentsOfFile:
substringWithRange:
InterceptionHope this helps lz
The key point of the problem is: Under the conditions of ANSI encoding, one Chinese character occupies two bytes and one English character occupies one byte .
So for your example:
So if you want to intercept "Hello a", then use:
If it is all in Chinese, for example:
Then if you want Chinese characters not to be truncated, you should at least read an even number of bytes.
This depends on the encoding. If the encoding standard is not certain, I am afraid that any software will read garbled characters.
...I’m not sure if I’m talking about the same thing as you...
It's nothing more than a problem with Chinese characters. You can directly take the length of the first 6 characters (regardless of Chinese and English, 6 characters are always enough), convert it into NSString, and then directly substringToIndex:3, take the first three characters, and it will come out. ?