C语言 如何在C中扫描完整的句子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18547004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:19:21  来源:igfitidea点击:

How to scanf full sentence in C

cstringscanf

提问by Dkova

How to scanf()full sentence in C without using fgets()or gets()

如何scanf()在不使用fgets()或的情况下用 C 完成句子gets()

I want to scan a command line from the user and based on the first word recognize the command. For example: mkdir

我想从用户扫描命令行并根据第一个单词识别命令。例如:mkdir

so I need to recognize that the user wants to create a new dir with the name "dir_name".

所以我需要认识到用户想要创建一个名为“dir_name”的新目录。

My code:

我的代码:

int main(){
  char *ch=malloc(sizeof("50");
  while (strcmp(ch,"exit")!=0){
     scanf("%[^\n]",ch);
  } 

when i ran this code after i input the first sentence and press enter it went to infinity loop i don't know why?

当我在输入第一句话并按 Enter 后运行此代码时,它进入无限循环,我不知道为什么?

回答by verbose

Your problem is most likely here:

您的问题很可能在这里:

char *ch=malloc(sizeof("50");

To begin with, you're missing one close parentheses. But assuming that this was a typo in posting the question and not in your actual code, there is a deeper issue.

首先,您缺少一个右括号。但是假设这是发布问题而不是您的实际代码中的错字,则存在更深层次的问题。

"50"inside double quotes is a string literal. When sizeof()is applied to the string literal, you're going to get the number of characters in the string, including terminating NUL. So you are only allocating space for three characters in ch.

"50"双引号内是字符串文字。当sizeof()应用于字符串文字时,您将获得字符串中的字符数,包括终止NUL. 所以你只为ch.

When you try to scanf()the input, you're writing past the end of your three-character buffer ch. Leave out sizeof()and simply say:

当您尝试scanf()输入时,您正在写入超过三字符缓冲区的末尾ch。离开sizeof()并简单地说:

char* ch = malloc (50);

Also, the scan set %[^\n]does notskip leading whitespace. Your first scanf()will stop at the newline, which will remain in the buffer. Subsequent scanf()calls in your whileloop will encounter that newline character and dutifully stop, as it's excluded from your scan set. So the loop condition

此外,扫描组%[^\n]没有跳过前导空白。您的第一个scanf()将停在换行符处,该换行符将保留在缓冲区中。循环中的后续scanf()调用while将遇到该换行符并尽职尽责地停止,因为它已从您的扫描集中排除。所以循环条件

while (strcmp (ch, "exit"))

will never become true, and you'll get an infinite loop. Consume the newline after the scanf()to avoid this problem:

永远不会变成真的,你会得到一个无限循环。在 之后使用换行符scanf()以避免此问题:

scanf ("%[^\n]%*c", ch);

The %*cmeans "read a character and then discard it." So it will read the \nthat is left in the buffer, and then not save it anywhere. Your next scanf()will therefore not encounter a \nat the beginning of the buffer, and your program will work as you intend.

%*c意思是“读取一个字符,然后丢弃它。” 所以它将读取\n留在缓冲区中的内容,然后不将其保存在任何地方。scanf()因此,您的 next不会\n在缓冲区的开头遇到 a ,并且您的程序将按您的预期工作。

回答by Humayun Kabbya

#include<stdio.h>
int main()
{
    char  input_string[100];
    scanf("%[^\n]s",&input_string);
    printf("Hello ,world.\n");
    printf("%s",input_string);
    return 0;
}

回答by charan raju

scanf("%[^\n]s",s); this will do you can also use scanf("%[^\n]%*c", s); where s is defined as char s[MAX_LEN] where MAX_LEN is the maximum size of s . Here, [] is the scanset character. ^\n stands for taking input until a newline isn't encountered. Then, with this %*c, it reads the newline character and here, the used * indicates that this newline character is discarded.

scanf("%[^\n]s",s); 这样做你也可以使用 scanf("%[^\n]%*c", s); 其中 s 定义为 char s[MAX_LEN] 其中 MAX_LEN 是 s 的最大大小。这里,[] 是扫描集字符。^\n 代表接受输入直到没有遇到换行符。然后,用这个 %*c,它读取换行符,在这里,使用的 * 表示这个换行符被丢弃。