博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WriteConsole使用实例
阅读量:7208 次
发布时间:2019-06-29

本文共 2141 字,大约阅读时间需要 7 分钟。

hot3.png

// crt_strlen.c

// Determine the length of a string. For the multi-byte character
// example to work correctly, the Japanese language support for
// non-Unicode programs must be enabled by the operating system.

#include <windows.h>

#include <string.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <mbstring.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>

int main()

{
   char* str1 = "Count.";
   wchar_t* wstr1 = L"Count.我是程序员小心";
   char * mbstr1;
   char * locale_string;
   TCHAR tch[4];
 
 tch[0] = 0x0061; // a
 
 tch[1] = 0x2014; // EM_DASH
 
 tch[2] = 0x0063; // b
 
 tch[3] = 0x0000; // NULL
   DWORD z;
   // strlen gives the length of single-byte character string
   printf("Length of '%s' : %d\n", str1, strlen(str1) );
  
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   if (hStdOut == INVALID_HANDLE_VALUE) return 1;
 
 DWORD dwBytesWritten;
 
 WriteConsole(hStdOut, wstr1, (DWORD)_tcslen(wstr1), &dwBytesWritten, NULL);
 
 WriteConsole(hStdOut, L"\n", 1, &dwBytesWritten, NULL);
   // wstrlen gives the length of a wide character string
   //WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),wstr1,sizeof(wstr1),&z,NULL);
   wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) );

   // A multibyte string: [A] [B] [C] [katakana A] [D] [\0]

   // in Code Page 932. For this example to work correctly,
   // the Japanese language support must be enabled by the
   // operating system.
   mbstr1 = "ABC" "\x83\x40" "D";

   locale_string = setlocale(LC_CTYPE, "Japanese_Japan");

   if (locale_string == NULL)

   {
      printf("Japanese locale not enabled. Exiting.\n");
      exit(1);
   }
   else
   {
      printf("Locale set to %s\n", locale_string);
   }

   // _mbslen will recognize the Japanese multibyte character if the

   // current locale used by the operating system is Japanese
   //printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) );

   // _mbstrlen will recognize the Japanese multibyte character

   // since the CRT locale is set to Japanese even if the OS locale
   // isnot.
   printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) );
   printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) );  
 
}

转载于:https://my.oschina.net/u/142173/blog/62834

你可能感兴趣的文章
modelform动态显示select标签的对象范围
查看>>
Android ---------- 富文本构建
查看>>
leetcode:Count Primes
查看>>
[转] babel的使用
查看>>
CentOS7.0安装与配置Tomcat-7
查看>>
C# SQL数据访问帮助类
查看>>
.net面试(汇总)
查看>>
.NET Entity Framework基本使用方法
查看>>
BZOJ3528: [Zjoi2014]星系调查
查看>>
Lua 随机数生成问题
查看>>
CLR的执行模型(4):执行程序集的代码
查看>>
同一脚本sh 脚本名 报Syntax error: "(" unexpected而./脚本名不报错,求解!!
查看>>
ZJOI2008皇帝的烦恼
查看>>
新手windows安装nginx
查看>>
浏览器兼容问题踩坑收集
查看>>
Python 实用技巧
查看>>
object c中@property 的使用
查看>>
Sping 核心IOC容器
查看>>
poj 2524
查看>>
MapReduce
查看>>