简体   繁体   English

用scanf C识别“空格”和“输入”

[英]Recognize "space" and "enter" with scanf C

I need to recognize if a character taken as input is SPACE or ENTER.我需要识别作为输入的字符是空格还是回车。 I know that enter is "0x0A" as hexadecimal while space is "0x20" but I don't kown why scanf seems to not recognize space.我知道 enter 是十六进制的“0x0A”,而空格是“0x20”,但我不知道为什么 scanf 似乎无法识别空格。

while ( (error=scanf("%d", &stop) )== EOF && error==0 )
  printf("Error while reading the value input, try again\n");
...(some code)...
while ( stop!= 0x0A )
{
    if (stop == 0x20) {
        printf("Going to fill the line\n");
    ...(some code)...
 }

With the 1st "while" I want that the user inserts a generic value, in the 2nd I check if the value was "ENTER" and the "if" checks if "SPACE" has been inserted.在第一个“while”中,我希望用户插入一个通用值,在第二个中我检查该值是否为“ENTER”,“if”检查是否已插入“SPACE”。 If I press "SPACE" there's a segmentation fault, don't know why :S如果我按“SPACE”,则会出现分段错误,不知道为什么:S

EDIT :编辑 :

I wrote this new example from what I read in comments :我根据我在评论中阅读的内容编写了这个新示例:

#include <stdio.h>
#include <stdlib.h>

void main()
{
    char input;
    int error =0;
    printf("I want to read only numbers\n" 
        "Let's start!\n");
    while ( (error=scanf("%c", &input) )== EOF || error==0 )
        printf("Error while reading the input, maybe Enter was pressed try again\n");
        printf("input is : %c \n",input);
        printf("Taking new input : \n");
   while (input != "\n")
   {
       if (input == 0x20)
          break;
          printf("Taking New input : \n");
       while ( (error=scanf("%c", &input) )== EOF || error==0 )
        printf("Error while reading the input, maybe Enter was pressed try again\n");
        printf("New input is : %c \n",input);
   }
   return;
}

this is the output :这是输出:

I want to read only numbers
Let's start!
7
input is : 7 
Taking New input : 
New input is : 

And the program ends.然后程序结束。

why scanf seems to not recognize space.为什么 scanf 似乎无法识别空间。

scanf() can recognized spaces, but not readily using scanf("%d", &stop) as "%d" first consumes and discards all leading white-space. scanf()可以识别空格,但容易使用scanf("%d", &stop)因为"%d"首先消耗并丢弃所有前导空格。

"%c" does not discard leading whitespace. "%c"不会丢弃前导空格。 One character is read.读取一个字符。


As OP seems interested in reading and testing a single character one at a time using scanf() , along with detecting a rare input error and maybe end-of-file:由于 OP 似乎有兴趣使用scanf()一次读取和测试一个字符,同时检测罕见的输入错误和文件结尾:

// Read one character.
// Return 1 on success.
// Return EOF on end-of-file.
// Return 0 on rare input error.
int read1(char *dest) {
  if (scanf("%c", dest) == 1) return 1;
  if (feof(stdin)) return EOF;
  return 0;
}

need to recognize if a character taken as input is SPACE or ENTER需要识别作为输入的字符是空格还是回车

fgets() is a much better approach to read a line of user input. fgets()是一个更好的方法来读取一行的用户输入。

char buf[100];
if (fgets(buf, sizeof buf, stdin)) {
  // Use buf
}  

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

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