原创 在Windows程序中,开启控制台收藏

新一篇: Learning boost 1 Serialization | 旧一篇: 类似stl中排列算法的组合算法

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

 

发表于 @ 2004年09月17日 22:02:00|评论(loading...)|编辑

新一篇: Learning boost 1 Serialization | 旧一篇: 类似stl中排列算法的组合算法

评论

#finallaugh 发表于2004-09-18 13:12:00  IP: 202.120.113.*
up
up
#y52 发表于2005-02-04 17:09:00  IP: 221.137.152.*
在 vc6.0 下试了,连编译也通不过
#Ery 发表于2005-11-10 11:49:00  IP: 210.22.177.*
为什么我调用
std::cout << "12325423153454236542362436";
控制台上没有任何反应哪?
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © heroboy2000