stdin没有阻塞的fgets的行为

时间:2016-08-05 13:14:40

标签: c++ fgets

fgets一起使用时,stdin的行为未被cout阻止。

/* Understanding fgets behaviour */

#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

int main() {

    char A[20], B[100];

    cin>>A;
    cout<<strlen(A)<<" = Length of A\n";

    // In this case programme stops for taking input for B 

    //cin>>B;
    //cout<<strlen(B)<<" = Length of B\n";

    /* But in this case , programme will 
     * not stop for taking input for B and 
     * exit with below output.(Mentioned end of the programme.) 
     */

    fgets(B,100,stdin);
    cout<<strlen(B)<<" = Length of B\n";

    return 0;
}

结果:

input:
asdf

output:
4 = Length of A
1 = Length of B

0 个答案:

没有答案