把工程改为Unicode的 来查看Unicode的txt文件 取出每一行的汉字

来源:互联网 发布:数据新闻报道比赛 编辑:程序博客网 时间:2024/05/03 05:08

 (1)把 Preprocessor Definitiond 写入UNICODE,_UNICODE (要选择一下默认的值这样才能改的生效)

 

(2)工程改成Unicode的

 

#ifdef _UNICODE
#define tstring std::wstring

#else
#define tstring std::string
#endif

 

实现过程:

readtxt.cpp:

#include "stdafx.h"
#include <stdio.h>
#include<fstream>


using namespace std;

tstring getline(FILE *stream)
{
 tstring strRet;
 TCHAR line[100] = {0};
 while( _fgetts( line, 100, stream ) != NULL)
 {
  if (line[0] == 0xFEFF) //unicode 文件的头
  {
   strRet += &line[1];
  }
  else
  {
   strRet += line;
  }
  if (line[_tcslen(line)-1] == _T('\n'))
  {
   break;
  }
 }
 return strRet;
}

void Trim( tstring& str , const tstring& chars =_T(" \t" ))
{
 str.erase(str.find_last_not_of(chars)+1);
 str.erase(0, str.find_first_not_of(chars));
}

int main(int argc,char **argv)
{
 FILE *stream = NULL;
 TCHAR szUserSuffix [20] = _T("E:\\1.txt");
 TCHAR  strQuickUser[100]={0};
 if( _wfopen_s( &stream,strQuickUser, _T("rt+,ccs=UNICODE ") ) == 0 )           //用到的函数都是多字节的;
 {
  tstring sRead;
  while (!feof(stream))
  {
   sRead = getline(stream);
   if (sRead.empty())
   {
    continue;
   }
   else
   {

    int n;
    n=sRead.find(_T("="));
    tstring s1 = _T("");
    tstring s2 = _T("");
    s1=sRead.substr(0,n);
    s2=sRead.substr(n+1,sRead.length()); 
    Trim(s1);
    Trim(s2);
    TCHAR tstrInitial[200];
    TCHAR tstrChinese[200];
    TCHAR tstrEspecial[200];
    bool flag = false;
    _tcscpy(tstrInitial ,s2.c_str());
    int j =0;

    for(int i=0; i<s2.length(); i++)
    {

     if ((tstrInitial[i]>=0x4E00)&&(tstrInitial[i]<=0x9FA5))
     {
      tstrChinese[j] = tstrInitial[i] ;
      tstrChinese[++j] = tstrInitial[++i];                   //汉字

      j++;
      flag = true;
     }
     else
     {
      if (flag==true)
      {
       break;
      }
      else
      {
       ;
      }     
     }

    }
    tstrChinese;

   }

}

 

原创粉丝点击