C++打印中文
来源:互联网 发布:c语言for语句continue 编辑:程序博客网 时间:2024/06/07 03:23
text.txt内容:
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <direct.h>#include<fstream>#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <windows.h>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ //读文件 ifstream fin("text.txt"); if(!fin) { cerr << "Error opening input stream" << endl; } char* buffer = (char*)malloc(6); fin.getline(buffer,6); fin.close(); //打印文件中的中文多字符不需要使用setlocate指定编码 printf("printf file string :%s\n",buffer); cout<<"cout file string :"<<buffer<<endl; //打印参数中的宽字符,输入参数为"中国人" wchar_t* arg = argv[1]; wprintf(L"wprintf setlocale before: %s\n",arg); //把宽字符转成多字符打印,这种转换方式会多出一些乱码字符 int len = wcslen(argv[1])*2; char* mbuf = (char*)malloc(len); wcstombs(mbuf,argv[1],len); printf("printf change buffer setlocale before:%s\n",mbuf); //无需调用setlocale也不会乱码 int szlen = ::WideCharToMultiByte(CP_ACP, 0, argv[1], -1, NULL, 0, NULL, NULL); char* nbuf = new char[szlen + 1]; ::WideCharToMultiByte(CP_ACP, 0,argv[1], -1, nbuf, szlen, NULL, NULL); cout<<"no need setlocale way:"<<nbuf<<endl; printf("printf nbuf:%s\n",nbuf); //必须使用setlocale指定编码,才不会乱 setlocale(LC_ALL, "Chinese-simplified"); printf("printf change buffer setlocale after:%s\n",mbuf); wprintf(L"setlocale after %s\n",arg); wcout<<"wcount buffer "<<arg<<endl; getchar(); return 0;}
输出结果:
代码下载:c++打印中文字符
0 0
- 打印中文
- 打印、注释、打印中文
- Win-TC打印C中文乱码问题解决原理详解
- C 打印
- wprintf打印中文
- Python打印中文问题
- 控制台打印中文
- C++打印中文
- ZPL打印中文
- Xcode 打印中文结果
- Texshop 打印中文方法
- _tprintf打印中文问题
- zebra中文打印
- 打印中文字符
- utuntu unicode打印中文
- Python_如何打印中文
- PAT (Basic Level) Practise (中文)1027. 打印沙漏(20) C语言
- 【C/C++】打印沙漏
- java基础知识:ArrayList和LinkedList的区别
- Shell管道学习 free -m|grep "Mem"|awk'{print$2,$4}'
- HDU 1429 三维visit bfs + 位运算优化
- HNACM(六)--第四题 探寻宝藏
- 坏道与坏扇区的区别
- C++打印中文
- ubuntu搭建本地源
- 单元格合并
- Android 混淆代码总结
- Android中自定义View(1)
- iOS开发笔记-cell里点击按钮展开收起-动态改变cell行高
- 【Android 应用开发】 ActionBar 样式详解 -- 样式 主题 简介 Actionbar 的 icon logo 标题 菜单样式...
- SpringMvc下载文件
- tar命令