zoukankan      html  css  js  c++  java
  • 如何输出_tmain中的argv[] 悲情土仔一生 C++博客

    如何输出_tmain中的argv[] - 悲情土仔一生 - C++博客

    如何输出_tmain中的argv[]


    作者:Tuuzed(土仔)  
    版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
    http://www.cppblog.com/tuuzed/archive/2008/02/23/43153.html





        使用过VS2005中VC++的大侠们应该对_tmain不陌生。是的,_tmain是Microsoft自己弄出来的东西,关于它的问题网上有一堆(GOOGLE Search)。今天想用VS2005中的VC++做一个WIN32控制台(console)下的应用程序,向导程序默认给出的是以下内容:
    1#include "stdafx.h"
    2
    3int _tmain(int argc, _TCHAR* argv[])
    4{
    5
    6    return 0;
    7
    8}

    9

    想当然,很好,很容易。试试列出命令的参数: 

     1#include "stdafx.h"
     2
     3using namespace std; //必须在stdafx.h增加#include <iostream>
     4
     5int _tmain(int argc, _TCHAR* argv[])
     6{
     7    cout << argc <<endl;
     8    cout << argv[0<<endl;
     9    return 0;
    10}

    11

    运行结果竟然是这样的: 

     

    D:\MyData\CSharp\Projects\test\Debug>test

    1

    003A5210

    003A5210”是什么值?一开始就弄不懂了。

    如果把_tmain函数变为: 

    1int main(int argc, char* argv[])
    2{
    3    cout << argc <<endl;
    4    cout << argv[0<<endl;
    5    return 0;
    6}

    7

    运行结果正常: 

     

    D:\MyData\CSharp\Projects\test\Debug>test

    1

    d:\MyData\CSharp\Projects\test\Debug\test.exe

    头大了。最后分别对两个函数运用断点中的反汇编看看,发现原来**argv竟然是wchar_t**,再翻弄了一下_TCHAR的声明:typedef wchar_t     _TCHAR;
    一切明白了。要输出这个_TCHAR只能用cout的另一个版本:wcout。
    为什么呢?原因很简单,因为他们都带了一个“w”在前面啊!

    能正确输出_TCHAR* argv[]的版本: 

     1#include "stdafx.h"
     2
     3using namespace std;
     4
     5int _tmain(int argc, _TCHAR* argv[])
     6{
     7    wcout << argc <<endl;
     8    wcout << argv[0<<endl;
     9    return 0;
    10}

    11




    给出一个网上对_tmain的一个有用的定义:
       
    对于ANSI版本,"_tWinMain"就是"WinMain";对于UINCODE版本,"_tWinMain"就是"wWinMain"。  

      (比如这样的定义:)    

     

    1  #ifdef   _UNICODE   
    2  #define   _tmain   wmain   
    3  #define   _tWinMain   wWinMain   
    4  #else   
    5  #define   _tmain   main   
    6  #define   _tWinMain   WinMain   
    7  #endif 

     

         所以,_tmain()不过是unicode版本的的main() 

  • 相关阅读:
    Spring EL Operators example
    Spring EL method invocation example
    Spring EL hello world example
    Spring @PostConstruct and @PreDestroy example
    Spring init-method and destroy-method example
    Define custom @Required-style annotation in Spring
    Spring dependency checking with @Required Annotation
    Spring properties dependency checking
    UESTC 883 方老师与两个串 --二分搜索+DP
    UESTC 882 冬马党 --状压DP
  • 原文地址:https://www.cnblogs.com/lexus/p/2590530.html
Copyright © 2011-2022 走看看