【心得】Ctrl+Z、 、 、eof的區別和用法

peixn發表於2019-05-14

從scanf談起:

一:scanf的返回值:讀入的域的個數

int scanf(
const char *format [,
argument]...
);
int _scanf_l(
const char *format,
locale_t locale [,
argument]...
);
int wscanf(
const wchar_t *format [,
argument]...
);
int _wscanf_l(
const wchar_t *format,
locale_t locale [,
argument]...
);

Return Value
Returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned.
If format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return EOF and set errno to EINVAL.
For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

// crt_scanf.c
// compile with: /W3
/* This program uses the scanf and wscanf functions
* to read formatted input.
*/
#include <stdio.h>
int main( void )
{
    int i, result;
    float fp;
    char c, s[81];
    wchar_t wc, ws[81];
    result = scanf( "%d %f %c %C %80s %80S", &i, &fp, &c, &wc, s, ws ); // C4996
    // Note: scanf and wscanf are deprecated; consider using scanf_s and wscanf_s
    printf( "The number of fields input is %d
", result );
    printf( "The contents are: %d %f %c %C %s %S
", i, fp, c, wc, s, ws);
    result = wscanf( L"%d %f %hc %lc %80S %80ls", &i, &fp, &c, &wc, s, ws ); // C4996
    wprintf( L"The number of fields input is %d
", result );
    wprintf( L"The contents are: %d %f %C %c %hs %s
", i, fp, c, wc, s, ws);
}


71 98.6 h z Byte characters
36 92.3 y n Wide characters


The number of fields input is 6
The contents are: 71 98.599998 h z Byte characters
The number of fields input is 6
The contents are: 36 92.300003 y n Wide characters

二、scanf對特殊符號的返回值

  • 自然數:符合輸入格式的資料的個數

  • 0:不符合輸入格式的資料(如果Ctrl+Z和Enter鍵之間有其他字元,則也返回0)

      以"%d"為例:要求是十進位制整數,如果輸入字元`r`、`e`、``等,就返回0,自然地,`
    `` ``eof`也返回0,這與他們本身的含義(比如換行符、終止符、檔案結束符)並無關係
  • -1:Ctrl+Z(緊接著按下Enter鍵)

三、輸入流中Ctrl+Z的含義

注:Windows系統中一般採用阻塞式檢查 Ctrl+Z、Unix/Linux系統下一般採用非阻塞式的檢查 Ctrl+D。本程式是在Windows系統下,因此使用阻塞式的 Ctrl+Z 來標識流的結束。

阻塞式方式的特點:

  1. 只有按下回車之後才有可能檢測在此之前是否有Ctrl+Z按下。

  2. (按照輸入時間順序讀取輸入緩衝區的資料)讀取到Ctrl+Z時,如果後面有可讀的資料,則不會理睬Ctrl+Z,也就是不認為Ctrl+Z代表著流的末尾。(因為有要讀的資料,還不能認為到了流的末尾)。

  3. Ctrl+Z產生的不是一個普通的ASCII碼值,也就是說它產生的不是一個字元,所以不會跟其它從鍵盤上輸入的字元一樣能夠存放在輸入緩衝區。

四、鍵盤輸入時Enter鍵的作用:

將鍵盤上敲下的字元送入輸入緩衝區。

如果使用者在按Enter鍵之前輸入了不只一個字元,其他字元會保留在鍵盤緩衝區中,等待後續的輸入函式(比如scanf()、getchar())呼叫讀取。也就是說,後續的scanf()呼叫不會等待使用者按鍵,而是直接讀取緩衝區中的字元,直到緩衝區的字元讀取完畢後,才等待使用者按鍵。

對於scanf(),只有字元char在輸入流中的獲取會承認空格或回車中的換行符為所要取的值,別的如字串或者字元陣列或int型別均不認為空格或回車中的換行符為其值即丟棄空格符和回車符,以空格作為劃分。

五、那換行符`
`、終止符` `、檔案結束符`eof`是幹嘛的

  1. `
    `、` `、`eof`是C/C++編譯器在編譯程式碼時識別的符號。

  2. Ctrl+Z是作業系統在處理輸入流時識別的符號。

`
`換行符,

#include <stdio.h>
int main()
{
    int c;
    do
    {
        printf("請輸入文件的結尾標誌");
    }while((c=getchar())!=`
`);
    printf("已得到文件結束標誌
");    //直接回車
    return 0;
} 

` `終止符
` ` is the null termination character. It marks the end of the string.

char cAlphabet[] = "I know all about programming!";

is the same as

char cAlphabet[] = {`I`,` `, `k`,`n`,`o`,`w`,` `,`a`,`l`,`l`,` `,`a`,`b`,`o`,`u`,`t`,` `,`p`,`r`,`o`,`g`,`r`,`a`,`m`,`i`,`n`,`g`,`!`,` `};
#include <stdio.h>
int main()
{
    int c;
    do
    {
        printf("請輸入文件的結尾標誌");
    }while((c=getchar())!=` `);
    printf("已得到文件結束標誌
");
    return 0;
} 

`eof`檔案結束符

#include <stdio.h>
int main()
{
    int c;
    do
    {
        printf("請輸入文件的結尾標誌");
    }while((c=getchar())!=EOF);
    printf("已得到文件結束標誌
");    //Ctrl+Z然後回車
    return 0;
} 

在控制檯輸入的時候,作業系統將Ctrl+Z翻譯為文件結束符。

相關文章