在Windows程序中,开启控制台

#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对控制台进行输入输出了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值