在Windows程式中,開啟控制台

來源:互聯網
上載者:User

#pragma once

template<int k>

class _Int2Type

{enum{_A=k};};

 

template <bool e1,bool e2> class _enable

{

public:

     static void enable()

     {

         es(_Int2Type<e1>());

         ei(_Int2Type<e2>());

     }

private:

     static void ei(_Int2Type<true>)

     {

         static std::ofstream conout("con");

         static std::ifstream conin("con");

         if (conout.is_open())

              std::cout.rdbuf(conout.rdbuf());

         if (conin.is_open())

              std::cin.rdbuf(conin.rdbuf());

     }

     static void ei(_Int2Type<false>){}

     static void es(_Int2Type<false>){}

     static void es(_Int2Type<true>)

     {

         FILE *pf;

         if (pf=fopen("con","rt"))

              _iob[0]=*pf;

         if (pf=fopen("con","wt"))

              _iob[2]=_iob[1]=*pf;

     }

};

template <bool _EnableStdio,bool _EnableIostream>

void OpenConsole()

{

     static bool first=true;

     if (first)

     {

         first=false;

         if (AllocConsole())

         {

              _enable<_EnableStdio,_EnableIostream>::enable();

         }

     }

}

 

使用方法:

OpenConsole<true,true>();

接下來就可以使用 cin,cout,printf,scanf對控制台進行輸入輸出了。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.