简体   繁体   English

我怎样才能使用scanf获得字符串呢?

[英]How can i get string using scanf as i want it to be?

How can i use scanf to read string with blank space( without enter )? 如何使用scanf读取带空格的字符串( 无需输入 )? And i also want this program to stop whenever input is EOF . 而且我也希望该程序在输入为EOF时停止

I used the following code: 我使用以下代码:

int main()      //this is not the whole program
{
    char A[10000];
    int length;

    while(scanf(" %[^\n]s",A)!=EOF);
    {
        length=strlen(A);
        print(length,A); 
        //printf("HELLO\n");
    }


    return 0;
}

But it is reading two EOF(ctrl+Z) to stop the program.can anyone give me any suggestion? 但是它正在读取两个EOF(ctrl + Z)来停止程序。有人可以给我任何建议吗?

it is reading two EOF(ctrl+Z) to stop the program 它正在读取两个EOF(ctrl + Z)以停止程序

No. You may be pressing ^Z twice, yet scanf() is only "reading" one end-of-file EOF . 否。您可能按了两次^ Z,但scanf()仅“读取”一个文件结尾的EOF That is just how your keyboard/OS interface works. 这就是您的键盘/ OS界面的工作方式。 Research how to signal end-of-file. 研究如何用信号通知文件结束。

Other changes 其他变化

char A[10000];
// while(scanf(" %[^\n]s",A)!=EOF);
// Drop final `;`  (That ends the while block)
// Add width limit
// Compare against the desired result, 1, not against one of the undesired results, EOF
// Drop the 's'
while(scanf(" %9999[^\n]", A) == 1) {
    length=strlen(A);
    // print(length,A); 
    print("%d <%s>\n", length, A); 
    //printf("HELLO\n");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM