WriteConsole使用实例

// 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值