标准输入、输出、错误设备


在ANSI C、Unix、Windows编程中,都定义有三个标准设备,即标准输入设备、标准输出设备以及标准错误设备。

标准输入设备通常是键盘
标准输出设备通常是屏幕
标准错误设备通常是屏幕


1)  标准设备
    在ANSI C编程中,三个标准设备分别是:stdin,stdout,stderr
    在stdio.h中有如下定义   
#define stdin  (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
    一个使用标准设备的例子如下
/*
 * FileName: Stand_C_Device.cpp 
 * Author:   JarvisChu
 * Date:     2012-11-07
 */

#include <stdio.h>

int main(int argc, char* argv[])
{
	int n;

	fscanf(stdin,"%d",&n);      //keyboard default
	fprintf(stdout,"%d\n",n);   //screen default
	fprintf(stderr, "This is stderr\n");//screen default
	
	return 0;
}


     2)  Windows定义有三个标准设备
        Windows编程中,也同样存在这样三个标准设备,只不过我们不用stdin这样的方式去使用,而是使用Windows API先获取这个三个标准设备的句柄,然后通过句柄去使用标准设备。
        通过Handle GetStdHandle(DWORD nStdHandle)来获取标准设备的句柄       
        nStdHandle :STD_INPUT_HANDLE
                     STD_OUTPUT_HANDLE
                     STD_ERROR_HANDLE   

     标准输入\输出\错误设备,默认也是键盘\屏幕\屏幕,但是我们可以通过调用函数 BOOL SetStdHandle(DWORD nStdHandle,Handle hHandle)来重置标准设备。
     
/*
 * FileName: Stand_Windows_Device.cpp 
 * Author:   JarvisChu
 * Date:     2012-11-07
 */

#include <stdio.h>
#include <windows.h>

#define BUFF_SIZE 256

int main(int argc, char* argv[])
{
	DWORD nRead;
	DWORD nWrite;
	TCHAR buf[BUFF_SIZE];
     
	HANDLE hStd_Input = GetStdHandle(STD_INPUT_HANDLE);   //Get the standard input device
	HANDLE hStd_Output = GetStdHandle(STD_OUTPUT_HANDLE); //Get the standard input device
	HANDLE hStd_Error = GetStdHandle(STD_ERROR_HANDLE);   //Get the standard error device

	//read from stdinput, and write to stdoutput
	while(ReadFile(hStd_Input,buf,BUFF_SIZE,&nRead,NULL) && nRead>0)
	{
		if(!WriteFile(hStd_Output,buf,nRead,&nWrite,NULL) || nRead != nWrite)
		{

			WriteFile(hStd_Error,"WriteFile Error",sizeof(TCHAR)*15,NULL,NULL);
		}
		break; //For the purpose of testing,just read one line
	}


        //--------------------------------------------------------------------------------------------
        //Standard Input Redirection

	HANDLE hFileIn = CreateFile("in.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
	
	//the two statements below complete the redirection
	SetStdHandle(STD_INPUT_HANDLE,hFileIn);               //set the hFileIn as the standard input device
	HANDLE newStd_Input = GetStdHandle(STD_INPUT_HANDLE); //Get the standard input device,now is hFileIn

	while(ReadFile(newStd_Input,buf,BUFF_SIZE,&nRead,NULL) && nRead>0)
	{
		WriteFile(hStd_Output,buf,nRead,&nWrite,NULL);
	}
	
	SetStdHandle(STD_INPUT_HANDLE,hStd_Input);  //Reset the Standard Input Device to default


	//--------------------------------------------------------------------------------------------
        //Standard Output Redirection

	HANDLE hFileOut = CreateFile("out.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);
	
	//the two statements below complete the redirection
	SetStdHandle(STD_OUTPUT_HANDLE,hFileOut);               //set the hFileIn as the standard input device
	HANDLE newStd_Output = GetStdHandle(STD_OUTPUT_HANDLE); //Get the standard output device,now is hFileout
	
	while(ReadFile(hStd_Input,buf,BUFF_SIZE,&nRead,NULL) && nRead>0)
	{
		WriteFile(newStd_Output,buf,nRead,&nWrite,NULL);
		break; //For the purpose of testing,just read one line
	} 

        //Reset the Standard IO Device to default
	SetStdHandle(STD_OUTPUT_HANDLE,hStd_Output);


	//--------------------------------------------------------------------------------------------
        //Close Handles
	CloseHandle(hFileIn);
	CloseHandle(hFileOut);
	CloseHandle(hStd_Input);
	CloseHandle(hStd_Input);
	CloseHandle(newStd_Input);
	CloseHandle(newStd_Output);

	return 0;
}
     其中in.txt中的内容如下     
博客地址
blog.csdn.net/jarvischu
程序运行效果如下:
标准输入、输出、错误设备_第1张图片


    3) Windows 控制台IO
       对于标准的输入输出设备,我们可以不用ReadFile WriteFile函数来操作,直接使用ReadConsole和WriteConsole来读写控制台,      
       使用SetConsoleMode来设置控制台的模式

/*
 * FileName: Windows_Console_IO.cpp 
 * Author:   JarvisChu
 * Date:     2012-11-07
 */

#include <stdio.h>
#include <windows.h>
#include <tchar.h>

#define BUFF_SIZE 256

int main(int argc, char* argv[])
{
	HANDLE hStdIn, hStdOut;
	TCHAR buf[BUFF_SIZE];
	DWORD nRead, nWrite;

	//CONIN$ is the default path of Console Input
	//CONOUT$ is the default path of Console Output
	hStdIn = CreateFile(_T("CONIN$"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    hStdOut = CreateFile(_T("CONOUT$"),GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

	SetConsoleMode(hStdIn,ENABLE_LINE_INPUT|ENABLE_WINDOW_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT|ENABLE_MOUSE_INPUT);
	SetConsoleMode(hStdOut,ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT);

	ReadConsole(hStdIn,buf,BUFF_SIZE,&nRead,NULL);
    WriteConsole(hStdOut,buf,nRead,&nWrite,NULL);

	CloseHandle(hStdIn);
	CloseHandle(hStdOut);
	return 0;
}


你可能感兴趣的:(标准输入、输出、错误设备)