一个C++按行读取Unicode 文本文件的例子

ExpandedBlockStart.gif 代码
#include  " stdafx.h "
#include 
< windows.h >
#include 
< stdio.h >
#include 
< cwctype >
#include 
< string >
#include 
< iostream >
using   namespace  std;


int  _tmain( int  argc, _TCHAR *  argv[])
{
    
if  (argc  ==   2 ) {
        FILE 
* fp  =  NULL;
        errno_t err 
=  _wfopen_s( & fp, argv[ 1 ], L " rb,ccs=Unicode " );
        
if  (err  !=   0 ) {
            cout 
<<   " open file  "   <<  argv[ 1 <<   " failed. error code:  "   <<  err  <<  endl;
        }

        wchar_t buf[
2048 =  { 0 };  //  假设每一行文本不超过2048字符
        size_t rdCount  =  fread(buf,  1 2 , fp);
        
if  (rdCount  !=   2 ) { fclose(fp);  return   1 ; }

        
if  (buf[ 0 !=   0xFF   ||  buf[ 1 !=   0xFE ) {
            fseek(fp, 
0 , SEEK_SET);  //  测试Unicode文件标志, 没有Unicode头,跳回文件头
        }

        
while  ( ! feof(fp)) {
            
if  (NULL  !=  fgetws(buf,  2048 , fp))
                wcout 
<<  buf  <<  endl;    
        }

        fclose(fp);
    }
    
return   0 ;
}

 

转载于:https://www.cnblogs.com/moonz-wu/archive/2010/04/24/1719040.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值