首页 > 其他分享 >fgetws 讀取Unicode文件 (zz.IS2120@BG57IV3)

fgetws 讀取Unicode文件 (zz.IS2120@BG57IV3)

时间:2023-06-09 17:03:59浏览次数:42  
标签:stream BG57IV3 text IS2120 mode Unicode 開啟 fgetws


//z 2012-11-22 18:48:32 [email protected][T4,L45,R0,V24]

fgetws 讀取Unicode文件



fgetws _fgetts 读取 中文 乱码 unicode 双字节 多字节



最近要讀取一個unicode文件做額外處理,但是透過 fgetws 去讀取檔案,利用WriteConsole顯示在console畫面卻無法正常顯示中文,查了好一陣子終於發現問題了;也就是在開啟檔案時你不可以使用 text mode去開啟檔案,必須透過binary mode開啟,這樣fgetws才會把檔案當作是unicode檔,也就是讀取時,會一次讀兩個bytes且不做任何轉換。



當你用text mode開啟時,fgetws會假設input stream是multibyte characters,所以會做 MBCS-to-Unicode 轉換。當你用text 開啟檔案用 fgetws讀取資料時,你會發現明明是一個中文字,但是讀取後會分開放到你設定的buffer中,例如下面,但是實際上我們期望的是0x540d。


buf[0] = 0x000d


buf[1] = 0x0054



//z 2012-11-22 18:50:40 [email protected][T5,L47,R1,V24]


這也是為啥讀出後直接丟給WriteConsole卻無法顯示中文的問題。



所以如果要處理unicode的檔案請使用 binary mode開啟,詳細解說你以參考 MSND 的


Unicode™ Stream I/O in Text and Binary Modes 文件。



ex:



FILE *inputfp;


const _TCHAR *pfilename = _T("MY UNICODE FILE");



//Don't use text mode to read a UNICODE file if you want use fgetws to read data from file.


errno_t err = _tfopen_s(&inputfp, pfilename,

_T("rb"));



_fgetts(Msgbuf, MAX_BUF_SIZE, inputfp);



DWORD dwCharWritten(0UL);



WriteConsole (GetStdHandle(STD_OUTPUT_HANDLE),


                             Msgbuf,


                             static_cast (_tcsclen(Msgbuf)),


                             &dwCharWritten,


                             NULL);



[Keyword]


_fgetts unicode file


//z 2012-11-22 18:50:40 [email protected][T5,L47,R1,V24]



When a Unicode stream I/O routine (such as fwprintf, fwscanf, fgetwc, fputwc, fgetws, or fputws) operates on a file that is open in text mode (the default), two kinds of character conversions take place:

  • Unicode-to-MBCS or MBCS-to-Unicode conversion. When a Unicode stream-I/O function operates in text mode, the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).
  • Carriage return – linefeed (CR-LF) translation. This translation occurs before the MBCS – Unicode conversion (for Unicode stream input functions) and after the Unicode – MBCS conversion (for Unicode stream output functions). During input, each carriage return – linefeed combination is translated into a single linefeed character. During output, each linefeed character is translated into a carriage return – linefeed combination.

However, when a Unicode stream-I/O function operates in binary mode, the file is assumed to be Unicode, and no CR-LF translation or character conversion occurs during input or output. Use the _setmode( _fileno( stdin ), _O_BINARY ); instruction in order to correctly use wcin on a UNICODE text file.



标签:stream,BG57IV3,text,IS2120,mode,Unicode,開啟,fgetws
From: https://blog.51cto.com/u_16156420/6449435

相关文章

  • 痛苦造就性格(也许还造就坏产品)(zz.IS2120)
    JamieZawinski//z2012-09-2622:22:[email protected][T50,L1560,R32,V428]他是一个非常传奇的人,高中都没有毕业,却是世界上最好的程序员之一,Netscape公司的主力程序员,划时代产品”网景浏览器”的主要开发者。他在公司里就是一身摇滚明星的打扮,留长发、穿皮夹克、戴......
  • Handling Invalid Characters in an XML String (zz.IS2120.BG57IV3)
    Thereare5predefinedentityreferencesinXML://z2013-08-2018:03:[email protected][T191,L2147,R75,V2925]<<lessthan>>greaterthan&amp;&ampersand &apos;'apostrophe""quotationmark//z2014-04-1017:47......
  • Jamie Zawinski访谈:在折腾中成长 (zz.IS2120)
    //z2012-09-2622:24:[email protected][T51,L1560,R32,V428]Zawinski:那终究只是理论。Seibel:是的,不过有时这个理论也能成真,只要主事者有良好的判断力,框架也不是太过精致,的确能节省时间。你能讲讲自己属于哪一类吗?Zawinski:虽然是陈词滥调,不过我还是要重提:更差就是......
  • 任正非的《北国之春》(zz.is2120)
    任正非的《北国之春》//z2012-09-1211:30:52IS212 [T36,L565,R10,V264]这是华为首席执行官任正非在出访和考察日本回国后所写的一篇文章,不仅华为内部员工熟读于心,而且也让众多的企业家、专家学者和大学生们奉为经典,即便是今日今时仍然具有很大的现实意义。我曾数百......
  • JGTM'2004 [MVP] 对MarshalByRefObject的讲解 (zz.is2120)
    //z2012-09-1109:30:[email protected][T30,L404,R7,V203] AnyonecanexplainthemajoruseofMarshalByRefObjectWhat'sthepurposeforMarshalByRefObject?1.Remoting;itmeansthatbetweenAppDomainsormachines,ratherthanserializea......
  • 转Unicode编码范围
    Unicode编码范围(Unicodeblocks)0x0000-0x001F:控制字符(Controlcharacter)0x0020-0x007F:基本拉丁字母(BasicLatin)0x0080-0x00FF:拉丁文补充1(Latin-1Supplement)0x0100-0x017F:拉丁文扩展-A(LatinExtended-A)0x0180-0x024F:拉丁文扩展-B(LatinExtended-B)0x0250-0x02AF:国际......
  • Jmeter-将响应数据从unicode编码转为中文
    现象: 解决:1. 修改配置 sampleresult.default.encoding=UTF-8 2. 添加后置处理器: BeanShell后置处理程序  可以在测试计划或者线程组下面添加,这样就不用每个请求都添加了. 代码:Strings=newString(prev.getResponseData(),"UTF-8");......
  • 浅谈字符集GB18030, GBK, GB2312, Unicode的适应性范围
    目前在中文世界里,计算机系统发展非常快速,传统的Windows已经逐渐跟不上国产化,如国产安卓系统,华为欧拉鸿蒙等系列,国产Linux系统等。国产化普遍支持GB18030!注:GB18030标准符合性认证一度属于国家强制性标准,由中国电子技术标准化研究所(CESI)认证中心进行授权认证。那么这些字符集......
  • 细说:Unicode, UTF-8, UTF-16, UTF-32, UCS-2, UCS-4
    1.Unicode与ISO10646全世界很多个国家都在为自己的文字编码,并且互不想通,不同的语言字符编码值相同却代表不同的符号(例如:韩文编码EUC-KR中“한국어”的编码值正好是汉字编码GBK中的“茄惫绢”)。因此,同一份文档,拷贝至不同语言的机器,就可能成了乱码,于是人们就想:我们能不能定义一......
  • 【IntelliJ IDEA】UTF-8编码下\u7528\u6237转换为中文汉字,\u9489\u9489\u81EA\u
    本文目录一、背景描述二、问题原因三、解决方案一、背景描述本地开发环境,Windows10+IntelliJIDEA+Springboot项目。在开发项目中遇见设置文件编码格式为UTF-8,但是打开该文件出现类似\u9489\u9489\u81EA\u5B9A\u4E49\u673A\u5668\u4EBA这样的数据,看也看不懂,也不是平常见到的......