C++ fopen、CFile如何以UTF-8编码格式读写文件

2023-11-05

 

How to write UTF-8 file with fprintf in C++

http://stackoverflow.com/questions/10028750/how-to-write-utf-8-file-with-fprintf-in-c

 

ou shouldn't need to set your locale or set any special modes on the file if you just want to use fprintf. You simply have to use UTF-8 encoded strings.

#include <cstdio>
#include <codecvt>

int main() {
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t> convert;
    std::string utf8_string = convert.to_bytes(L"кошка 日本国");

    if(FILE *f = fopen("tmp","w"))
    fprintf(f,"%s\n",utf8_string.c_str());
}

 

Save the program as UTF-8 with signature or UTF-16 (i.e. don't use UTF-8 without signature, otherwise VS won't produce the right string literal). The file written by the program will contain the UTF-8 version of that string. Or you can do:

int main() {
    if(FILE *f = fopen("tmp","w"))
        fprintf(f,"%s\n","кошка 日本国");
}

 

In this case you must save the file as UTF-8 without signature, because you want the compiler to think the source encoding is the same as the execution encoding... This is a bit of a hack that relies on the compiler's, IMO, broken behavior.

You can do basically the same thing with any of the other APIs for writing narrow characters to a file, but note that none of these methods work for writing UTF-8 to the Windows console. Because the C runtime and/or the console is a bit broken you can only write UTF-8 directly to the console by doing SetConsoleOutputCP(65001) and then using one of the puts variety of function.

If you want to use wide characters instead of narrow characters then locale based methods and setting modes on file descriptors could come into play.

#include <cstdio>
#include <fcntl.h>
#include <io.h>

int main() {
    if(FILE *f = fopen("tmp","w")) {
        _setmode(_fileno(f), _O_U8TEXT);
        fwprintf(f,L"%s\n",L"кошка 日本国");
    }
}

 

#include <fstream>
#include <codecvt>

int main() {
    if(auto f = std::wofstream("tmp")) {
        f.imbue(std::locale(std::locale(),
                new std::codecvt_utf8_utf16<wchar_
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ fopen、CFile如何以UTF-8编码格式读写文件 的相关文章

随机推荐

  • 入门ResNet,在Cub200数据集上复现Resnet50

    1 背景问题 1 如果只是单纯地把卷积层和池化层进行堆叠 造成的问题就会有梯度消失和梯度爆炸 梯度消失是指当在某一层进行BP的时候 误差为一个小于零的数 那不断相乘 就会趋近于零 梯度爆炸则是指某一层的开始误差都是大于1的数 直接相乘就会导
  • centos7 sh 注释_shell 中的单行注释和多行注释

    导读 关于 shell 中的单行注释和多行注释的问题 本文档介绍两种实用的方法 1 单行注释 众所周知 比如想要注释 echo Hello World root Jaking vim test sh echo Hello World 2 多
  • 如何进行需求测试/需求评审

    由于软件系统的复杂性 在需求分析阶段可能存在着开发方对委托方业务需求理解不全面 不准确的情况 在这种情况下 如果不进行相关的质量控制 往往会造成开发结果与用户需求不一致的后果 需求测试的目的就在于保证软件设计最大可能地满足有关用户的所有需求
  • 从前端传来的JSON中获取数据

    首先推荐一个神器 JSON在线解析及格式化验证 JSON cn 里面的 JSON在线解析 和 JSON生成JAVA实体 两个功能 前几天可是帮了我大忙了 前几天写一个功能 在这个功能中前端传过来的JSON十分复杂 示例如 Dispositi
  • virtualbox 安装centos7之后无法ssh登陆

    文章目录 virtualbox 安装 centos7 开启centos7网络 sshd 服务是否开启 设置 virtualbox 端口转发功能 设置secureCrt 连接参数 virtualbox 安装 centos7 virtualbo
  • 贝叶斯网络与R语言

    贝叶斯网络与R语言 基本语句 1 1网络的创建 加载扩展包和bnlearn包自带数据集marks 数据集marks 88 学生5门课的成绩 MECH mechanics VECT vectors ALG algebra ANL analys
  • 十一. Kubernetes 容器 container 设置详解

    目录 一 基础解释 yaml设置容器拉取镜像注意点 1 containers image 镜像 2 containers imagePullPolicy 镜像拉取策略 3 配置拉取私库镜像 spec下的imagePullSecrets 4
  • 【六级单词】

    affordable 价格合理的 cash 现金 insurance 保险 forune 一大笔钱 机会 运气 misfortune 不幸 灾难 luxury 奢侈 豪华 luxurious shop pension 养老金 抚恤金 com
  • C语言每日一题:16:数对。

    思路一 基本思路 1 x y均不大于n 就是小于等于n 2 x y大于等于k 3 一般的思路使用双for循环去遍历每一对数 代码实现 include
  • pytorch霹雳巴拉——图像分类篇

    up给的教程路线 图像分类 目标检测 一步步学习用pytorch实现深度学习在cv上的应用 并做笔记整理和总结 参考内容来自 up主的b站链接 https space bilibili com 18161609 channel index
  • layui 动态加载 select

    感谢小张帅三代以及他的好文 layui ajax select 动态添加数据方法 给我指明了前进的方向 首先 这是一个学习的过程 并不是最优方案 只是 玩索而有得 而己 做了一个联动的搜索框 本来一开始想用layuiselect第三方插件
  • 图的遍历方法——DFS和BFS

    DFS类似于树的先序遍历 因此可以用递归实现 BFS类似于树的层次遍历 因此可以用队列实现 说明 下面代码中图的存储方式是邻接表 关于邻接表和邻接矩阵可看邻接表和邻接矩阵 1 深度优先遍历 Depth First Search 思想 从图中
  • 微信小程序实现单/多图片上传(预览删除)

    wxml结构 上传图片
  • Linux中Vim文件夹路径,一些有用的Linux命令和Vim使用总结

    常见Linux命令 文件复制 移动 删除 创建 复制 cp v 源文件路径 目标文件路径 移动 mv v 源文件路径 目标文件路径 删除 rm v 文件路径 rmdir v 文件夹路径 文件夹要为空 rm rv 文件夹路径 递归删除文件夹及
  • Qt界面开发(一)(各种控件以及图表)

    注 资源主要来源 http www qtcn org bbs u 110085 刘大神 如若侵权 请联系删除 本文只是将作品集合到起来 方便大家一起学习 资源集合已经放到 链接 https pan baidu com s 1sVvQE8uD
  • ts 因为在此系统上禁止运行脚本(win10系统)

    今天弄了一下Ts 有点晚了 但是确实是才开始尝试 以前只是看了看 1 先安装 npm install g typescript 2 安装成功 typescript 4 0 3 added 1 package from 1 contribut
  • Goby的使用 漏洞扫描工具

    获取自己虚拟机的ip 打开Goby 点击扫描 输入虚拟机的IP地址 开始扫描 扫描结束这里没有扫到漏洞 点击报告查看报告 右上角下载生成报告 漏洞举例
  • C++学习笔记之浅拷贝&深拷贝的理解

    一 浅拷贝 浅拷贝就是把类中的成员属性简单的复制 如果有指针成员变量 也只是拷贝指针的地址 下面案例就是先创建teacher1对象 再把它初始化给teacher2对象 在初始化时需要调用复制构造函数 因为Teacher类没有重写复制构造函数
  • 使用docker搭建一个完全分布式的hadoop集群

    项目地址 https github com czfshine docker hadoop docker hadoop A dockerfile for setting up a full Hadoop cluster server 一套在u
  • C++ fopen、CFile如何以UTF-8编码格式读写文件

    How to write UTF 8 file with fprintf in C http stackoverflow com questions 10028750 how to write utf 8 file with fprintf