简体   繁体   English

用逗号读取输入(使用 sscanf)

[英]Read input with commas (with sscanf)

Hi I'm trying to read the input of a user in the following form:您好我正在尝试以下列形式读取用户的输入:

param1,param2,param3

that I get with fgets .我用fgets得到的。 The problem is that I can't seem to understand why it doesn't, after the first comma it shows me a weird behaviour.问题是我似乎无法理解为什么它没有,在第一个逗号之后它向我展示了一种奇怪的行为。 Ex:前任:

char input[60];
char a1, a2, a3, a4;


printf("Enter info");
fgets(input, 60, stdin);
sscanf(input,"%[^,\n] %[^,\n] %[^,\n] %[^,\n]", &a1, &a2, &a3, &a4);
printf("%c %c %c %c", a1, a2,a3, a4);

If I type for example a,b,c,d I get: a � What am I missing here?如果我输入例如a,b,c,d我会得到: a � 我在这里错过了什么?

Adding to the answer.添加到答案。 Your format string is missing the actual , commas, so 1,2,3,4 cannot match it.您的格式字符串缺少实际的,逗号,因此1,2,3,4无法匹配。

You can do something like this你可以做这样的事情

#include <stdio.h>

int main()
{
  char input[60];
  char a1[2], a2[2], a3[2], a4[2];


  printf("Enter info");
  fgets(input, 60, stdin);
  if (4 != sscanf(input,"%1[^,\n], %1[^,\n], %1[^,\n], %1[^,\n]", a1, a2, a3, a4))
    return printf("Error"), 1;

  printf("%c %c %c %c", a1[0], a2[0],a3[0], a4[0]);
  return 0;
}

Note the error checking with sscanf if it was successful it should return the number of variables which it filled with data.请注意使用sscanf进行的错误检查,如果成功,它应该返回它填充数据的变量数。 It must be 4 if it worked.如果有效,它必须是 4。

Change the array size of your parameter and the number after the % in the sscanf format string to match your needs.更改参数的数组大小和sscanf格式字符串中%后的数字以符合您的需要。

Edit: To make it throw an error if the input has too much params you can do this编辑:如果输入有太多参数,要让它抛出错误,你可以这样做

#include <stdio.h>

int main()
{
  char input[60];
  char a1[2], a2[2], a3[2], a4[2];
  char temp[2];

  printf("Enter info");
  fgets(input, 60, stdin);
  if (4 != sscanf(input,"%1[^,\n], %1[^,\n], %1[^,\n], %1[^,\n] %1[^\n]", a1, a2, a3, a4, temp))
    return printf("Error"), 1;

  printf("%c %c %c %c", a1[0], a2[0],a3[0], a4[0]);
  return 0;
}

By adding %1[^\n] to the end of the format sscanf will try to read in another non whitespace character after the four parameter.通过将%1[^\n]添加到格式的末尾, sscanf将尝试在四个参数之后读取另一个非空白字符。 If it succeed that mean there is too much parameter in the input for ex: 1,2,3,4,5 .如果成功,则表示输入中的参数过多,例如: 1,2,3,4,5 It will return 5 which is the number of data it was able to fill.它将返回 5,这是它能够填充的数据数。 But this last format specifier should not match anything if the input is valid, so sscanf should return exactly 4 in case of a valid input.但是,如果输入有效,则最后一个格式说明符不应匹配任何内容,因此sscanf应在有效输入的情况下准确返回 4。

The problem is that the %[ format specifier appends a null character as it consumes a sequence of characters.问题是%[格式说明符附加了一个 null 字符,因为它消耗了一个字符序列。

It needs to have an appropriate argument of type char[] capable of holding at least the character plus the null character, but the relative arguments of each %[ is only of type char at your example.它需要有一个适当的char[]类型的参数,至少能够保存字符加上 null 字符,但是每个%[的相对 arguments 在您的示例中仅属于char类型。

This is causing undefined behavior, as the appending \0 is written beyond the bounds of the array.这会导致未定义的行为,因为附加的\0写入超出了数组的范围。

If you want to read and print comma separated word you can use:如果您想阅读和打印逗号分隔的单词,您可以使用:

#include <stdio.h>

int main()
{
  char input[60];
  char a1[15], a2[15], a3[15], a4[15];

  for(int i; i < 10; i++)
  {
      a1[i] = a2[i] = a3[i] = a4[i] = 0;  // Initialize each character with `\0`.
  }

  printf("Enter info\n");
  fgets(input, 60, stdin);
  sscanf(input,"%[^,\n],%[^,\n],%[^,\n],%[^\n]", a1, a2, a3, a4);


  printf("%s %s %s %s", a1, a2, a3, a4);
  return 0;
}

Execution:执行:

/a.out
hello,mister,tamborine,man
hello mister tamborine man

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

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