標準輸入、輸出、錯誤裝置

來源:互聯網
上載者:User
在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 defaultfprintf(stdout,"%d\n",n);   //screen defaultfprintf(stderr, "This is stderr\n");//screen defaultreturn 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 256int main(int argc, char* argv[]){DWORD nRead;DWORD nWrite;TCHAR buf[BUFF_SIZE];     HANDLE hStd_Input = GetStdHandle(STD_INPUT_HANDLE);   //Get the standard input deviceHANDLE hStd_Output = GetStdHandle(STD_OUTPUT_HANDLE); //Get the standard input deviceHANDLE hStd_Error = GetStdHandle(STD_ERROR_HANDLE);   //Get the standard error device//read from stdinput, and write to stdoutputwhile(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 RedirectionHANDLE hFileIn = CreateFile("in.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);//the two statements below complete the redirectionSetStdHandle(STD_INPUT_HANDLE,hFileIn);               //set the hFileIn as the standard input deviceHANDLE newStd_Input = GetStdHandle(STD_INPUT_HANDLE); //Get the standard input device,now is hFileInwhile(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 RedirectionHANDLE hFileOut = CreateFile("out.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);//the two statements below complete the redirectionSetStdHandle(STD_OUTPUT_HANDLE,hFileOut);               //set the hFileIn as the standard input deviceHANDLE newStd_Output = GetStdHandle(STD_OUTPUT_HANDLE); //Get the standard output device,now is hFileoutwhile(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 defaultSetStdHandle(STD_OUTPUT_HANDLE,hStd_Output);//--------------------------------------------------------------------------------------------        //Close HandlesCloseHandle(hFileIn);CloseHandle(hFileOut);CloseHandle(hStd_Input);CloseHandle(hStd_Input);CloseHandle(newStd_Input);CloseHandle(newStd_Output);return 0;}

     其中in.txt中的內容如下     

部落格地址blog.csdn.net/jarvischu
程式運行效果如下:


    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 256int 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 OutputhStdIn = 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.