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

本文介绍了一个使用C++处理Unicode文本文件的方法。通过预处理器定义支持Unicode,并展示了如何从文本文件中读取Unicode内容,同时进行必要的字符串修剪和字符筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值