In Windows, enable the Console

Source: Internet
Author: 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 ();

}

}

}

 

Usage:

Openconsole <true, true> ();

Then you can use Cin, cout, printf, and scanf to input and output the console.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.