c++ 读取UTF-8编码文本

2023-05-16

这个是苏州大学一个有关NLP的选修课的第一个作业,刚开始有点摸不着头脑,随着慢慢接触有点理解了老师的用心。

任务是给定一个给一段汉语文本,将文本切分开为单个character,并在character中间填充上空格,以确认字符识别的效果。

刚开始我是想着把结果从控制台中输出出来,但是靠平常使用的基本库是无法做到这一点的,因为在UTF8编码时,汉字一般需要三个字节,而在GBK编码中中文一般占两个字节。想要识别是汉字还是英文或是数字这个比较容易,问题就在于怎么把识别完的汉字输出出来?如果文本是UTF8编码,我可以很容易地把代表一个汉字的三个字节数据拿出来,但是我无法告诉程序这个汉字是UTF8编码的,你把这三个字节拿去给我按照这个编码找到个汉字回来!如果不调用其他的库,那这基本不太可能做到,所以我换了个思路,不把character从控制台输出了,直接把character输出到文件里,到时候一打开文件就能看到结果对不对,而且由于我每向文件输入一个character都会同时再向文件输入一个空格作为标记,证明这个文本确实是我读取并用空格分隔开的。

对于思路的解释我等会再更新,代码先放上。我总共写了两个读取的思路,有一点小小的不同,大家可以看看。

方法一代码:

//方法1
bool UTF8Reader_Approch_1(string fileName)  
{
	int byte_110 = 3 << 6;
	int byte_mark_110 = 7 << 5;
	int byte_1110 = 7 << 5;
	int byte_mark_1110 = 15 << 4;
	int byte_11110 = 15 << 4;
	int byte_mark_11110 = 31 << 3;
	int byte_111110 = 31 << 3;
	int byte_mark_111110 =63<< 2;
	int byte_1111110 = 63 << 2;
	int byte_mark_1111110 = 127 << 1;

	FILE *fp=NULL;
	fp = fopen(fileName.c_str(),"r");
	FILE *output = NULL;
	output = fopen("ToWrite.txt","w");
	int byte =fgetc(fp);
	while (byte!=EOF)
	{
		if ((byte&byte_mark_110) == byte_110)
		{
			char str[3];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = '\0';
			fprintf(output, "%s ", str);
		}
		else if ((byte&byte_mark_1110) == byte_1110)
		{
			char str[4];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = '\0';
			fprintf(output, "%s ", str);
		}
		else if ((byte&byte_mark_11110) == byte_11110)
		{
			char str[5];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = '\0';
			fprintf(output, "%s ", str);
		}
		else if ((byte&byte_mark_111110) == byte_111110)
		{
			char str[6];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = fgetc(fp);
			str[5] = '\0';
			fprintf(output, "%s ", str);
		}
		else if ((byte&byte_mark_1111110) == byte_1111110)
		{
			char str[7];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = fgetc(fp);
			str[5] = fgetc(fp);
			str[6] = '\0';
			fprintf(output, "%s ", str);
		}
		else
		{
			char str[2];
			str[0] = byte;
			str[1] ='\0';
			fprintf(output, "%s ", str);
		}
		byte =fgetc(fp);
	}
	fclose(fp);
	fclose(output);
	return true;
}

方法二代码:

//方法2
bool UTF8Reader_Approch_2(string fileName)  
{
	FILE *fp = NULL;
	fp = fopen(fileName.c_str(), "r");
	FILE *output = NULL;
	output = fopen("ToWrite.txt", "w");
	if (fp == NULL || output == NULL)
	{
		return false;
	}
	char byte = fgetc(fp);
	unsigned char mask = 255;
	while (byte != EOF)
	{
		int result = mask & byte;
		if (result < 128)  //1字节
		{
			char str[2];
			str[0] = byte;
			str[1] = '\0';
			fprintf(output, "%s ", str);
		}
		else if (result >= 192 && result <= 223)  //2字节
		{
			char str[3];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = '\0';
			fprintf(output, "%s ", str);
		}
		else if (result >= 224 && result <= 239)  //3字节
		{
			char str[4];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = '\0';
			fprintf(output, "%s ", str);
		}
		else if (result >= 240 && result <= 247)  //4字节
		{
			char str[5];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = '\0';
			fprintf(output, "%s ", str);
		}
		else if (result >= 248 && result <= 251)  //5字节
		{
			char str[6];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = fgetc(fp);
			str[5] = '\0';
			fprintf(output, "%s ", str);
		}
		else if (result >= 252 && result <= 253)  //6字节
		{
			char str[7];
			str[0] = byte;
			str[1] = fgetc(fp);
			str[2] = fgetc(fp);
			str[3] = fgetc(fp);
			str[4] = fgetc(fp);
			str[5] = fgetc(fp);
			str[6] = '\0';
			fprintf(output, "%s ", str);
		}
		byte = fgetc(fp);
	}
	fclose(fp);
	fclose(output);
	return true;
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c++ 读取UTF-8编码文本 的相关文章

随机推荐

  • 利用在线词典批量查询英语单词

    进来遇到很多英语生词 xff0c 工具书上给的解释错误百出 xff0c 而很多在线词典不但可以给出某个单词的解释 xff0c 而且有大量的示例 xff0c 因此猜想利用在线词典批量查询这些单词 怎么实现呢 xff1f 首要问题是如何自动获取
  • linux svn服务器搭建 centos 搭建svn服务器

    本文是在CentOS中采用yum安装方式 优点 xff1a 简单 xff0c 一键安装 xff0c 不用手动配置环境变量等 缺点 xff1a 安装位置为yum默认 xff0c 比如我们公司服务器上安装软件有自己的规定 xff0c 一般会采用
  • Firewall 防火墙常用命令

    Firewall开启常见端口命令 xff1a 注意 permanent意思是 永久生效 firewall cmd zone 61 public add port 61 80 tcp permanent firewall cmd zone 6
  • 第二章——keil5修改工程名字

    第一章 stm32f103建立工程 第二章 keil5修改工程名字 目录 1 修改模板文件名 2 修改工程文件名 3 删除中间文件 4 修改输出中间变量文件名 5 点击编译 xff0c 改名成功 1 修改模板文件名 把第一章建立的工程模板的
  • origin2021如何切换中文界面

    origin2021如何切换中文界面 一 直接设置Change Language二 Change Language菜单是灰色的 一 直接设置Change Language 1 单击 Help gt Change Language 2 将La
  • fbe 业务流程分析

    参考链接 xff1a https www cnblogs com bobfly1984 p 14090078 html 总结 根据 data unencrypted key和 data misc vold user keys de 0 路径
  • js的字符串匹配方法match()和Java的字符串匹配方法matches()的使用?以换行符替换为其他字符为例

    js的字符串匹配方法match 和Java的字符串匹配方法matches 的使用 xff1f 以换行符替换为其他字符为例 js的 xff1a str match n igm length会返回str中有多少个换行str match bc i
  • UNIX 环境高级编程

    与你共享 xff0c 与你共舞 xff01 UNIX环境高级编程 xff08 第3版 xff09 是被誉为UNIX编程 圣经 xff1b 书中除了介绍UNIX文件和目录 标准I O库 系统数据文件和信息 进程环境 进程控制 进程关系 信号
  • 华为服务器WebBios创建磁盘阵列

    步骤 1 启动服务器按ctrl 43 h进入WebBios 2 点击Start确定进入下一步 3 左栏的Configuration Wizard添加raid 4 选New Configuration新建raid即可 5 选中硬盘 然后再按N
  • goland 无法编译输出 Compilation finished with exit code 0

    golang编写程序无法输出
  • 分享关于AI的那些事儿

    机器人很厉害 给人治病的ibm 的Watson 沃森 击败世界围棋冠军的AlphaGo阿尔法狗 陪你聊天的机器人 数据标注 木马识别 恶意访问拦截 智能家居 但是17年首次出现了机器人获得国籍 这个机器人叫做索菲亚 这是一个类似人类的机器人
  • String Evolver, My First Genetic Algorithm

    When reading Evolutionary Computation for Modeling and Optimization 1 I found following problem in section 1 2 3 A strin
  • MongoDB特点及功能介绍

    一 MongoDB 介绍 1 基本概念 MongoDB是一个高性能 xff0c 开源 xff0c 无模式的文档型数据库 xff0c 是当前NoSQL数据库产品中最热门的一种 它在许多场景下可用于替代传统的关系型数据库或键 值存储方式 xff
  • 线程同步以及线程调度相关的方法

    wait xff1a 使一个线程处于等待 xff08 阻塞 xff09 状态 xff0c 并且释放所持有的对象的锁 xff1b sleep xff1a 使一个正在运行的线程处于睡眠状态 xff0c 是一个静态方法 xff0c 调用此方法要处
  • 智能医疗辅助诊断——调查与思考

    背景 为什么要做智能医疗 xff1f 优质医疗资源不足且增长缓慢各地方医疗资源分配不均客观条件满足 xff0c 人工智能技术发展 xff0c 算法 算力 数据齐备 目录 指出 xff0c 医用软件按照预期用途分为辅助诊断类和治疗类 诊断功能
  • WebMvcConfigurer配置HandlerInterceptor拦截器失效

    1 前言 Springboot2 0之前 xff0c 实现拦截器功能的配置类是通过继承 extends WebMvcConfigurerAdapter类完成的 xff0c 最近项目把Springboot升级到了Springboot2 X x
  • ubuntu deepin wechat中文乱码解决

    deepin wechat 中文乱码解决方案 方案一 执行以下命令打开文件 gedit opt deepinwine tools run sh 找到WINE CMD 修改为 WINE CMD span class token operato
  • 使用k-近邻算法识别手写数字

    本文摘自 机器学习实战 案例 xff0c 对其进行了代码更新与注释 实战介绍 使用k 近邻分类器构造手写识别系统 xff0c 为了简单起见 xff0c 系统只识别0 9 xff0c 需要识别的数字已经使用图形处理软件 xff0c 处理成具有
  • Android开发:Fragment中优雅使用ViewBinding【Java】

    目录 前言 官网示例 封装 前言 ViewBinding可以帮助我们减少代码中的大部分findViewById xff0c 官网中提到了它的优点和缺点 xff1a Null 安全 xff1a 由于视图绑定会创建对视图的直接引用 xff0c
  • c++ 读取UTF-8编码文本

    这个是苏州大学一个有关NLP的选修课的第一个作业 xff0c 刚开始有点摸不着头脑 xff0c 随着慢慢接触有点理解了老师的用心 任务是给定一个给一段汉语文本 xff0c 将文本切分开为单个character xff0c 并在charact