【问题标题】:C scanf() scansets to read comma separated value (string and integer) from consoleC scanf() 扫描集从控制台读取逗号分隔值(字符串和整数)
【发布时间】:2021-05-10 22:46:40
【问题描述】:

我有以下任务:

读取以下格式的逗号分隔数据:
string1, string2, integer_number
例如:

  Hi, Hello, 123
  John, Doe, 45

我编写了以下代码来处理这种情况。 问题是我必须区分以下几种情况:

  • asd, asd, asd
  • asd, asf
    但是在这两种情况下,scanf 返回值2 这是正确的行为,因为据我了解,首先没有给出整数,其次没有第三个值。 所以,我需要一些帮助,如何将扫描集设置为仅在包含一些文本和逗号 - >“文本”时作为正确的字符串数据传递。 或者我应该使用与scanf()不同的功能。

使这项任务更难的是要求使用任何由 [] 声明或动态分配的附加数组。这必须仅使用这 3 个变量来完成。不允许使用其他缓冲区。

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

int main()
{
    char string1[20];
    char string2[20];
    int number;
    printf("Type data:\n");
    int ret_val = scanf("%[a-zA-Z], %[a-zA-Z], %d", string1, string2, &number);
    if (ret_val != 3) {
        printf("ret_val %d", ret_val);
        return 1;
    }
    printf("%s %s %d\n", string1, string2, number);
    return 0;
}

【问题讨论】:

  • 那么这两种情况应该怎么办?
  • asd, asd, asd - 应该认识到只有 string1 和 string2 被正确读取。 asd, asf - 应该认识到只有 string1 被正确读取
  • scanf 将返回 2,所以这意味着只有 string1 和 string2 被正确读取......并且......?也许我不明白你在追求什么......
  • 每个字符串必须以逗号结尾
  • 不,因为没有您可以使用的“功能”。您必须自己编写代码。

标签: c string scanf stdin


【解决方案1】:

我通常使用*scanf() 的技巧是使用%n 来检测输入是否已被读取;这在您期望一些文字时特别有用(*scanf() 的结果中不考虑它们)。

请注意,根据man 3 sscanf%n 不考虑在*scanf() 的结果中。

/**
  gcc -std=c99 -o prog_c prog_c.c \
      -pedantic -Wall -Wextra -Wconversion \
      -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla \
      -g -O0 -UNDEBUG -fsanitize=address,undefined
**/

#include <stdio.h>

void
parse_line(const char *line)
{
  printf("[%s] --> ", line);
  char string1[20]="";
  char string2[20]="";
  int number=-1;
  int n1=-1, n2=-1;
  int r=sscanf(line, "%[a-zA-Z],%n %[a-zA-Z],%n %d",
               string1, &n1, string2, &n2, &number);
  switch(r)
  {
    case 3:
    {
      printf("3 fields extracted: %s %s %d\n", string1, string2, number);
      break;
    }
    case 2:
    {
      if(n2!=-1) // this coma has been read
      {
        printf("incorrect third field, ");
      }
      printf("2 fields extracted: %s %s\n", string1, string2);
      break;
    }
    case 1:
    {
      if(n1!=-1) // this coma has been read
      {
        printf("incorrect second field, ");
      }
      printf("1 field extracted: %s\n", string1);
      break;
    }
    default:
    {
      printf("no field extracted\n");
    }
  }
}

int
main(void)
{
  parse_line("Hi, Hello, 123");
  parse_line("John, Doe, 45");
  parse_line("asd, asd, asd");
  parse_line("asd, asf");
  parse_line("asd, ");
  parse_line("asd");
  parse_line("");
  return 0;
}
/**
[Hi, Hello, 123] --> 3 fields extracted: Hi Hello 123
[John, Doe, 45] --> 3 fields extracted: John Doe 45
[asd, asd, asd] --> incorrect third field, 2 fields extracted: asd asd
[asd, asf] --> 2 fields extracted: asd asf
[asd, ] --> incorrect second field, 1 field extracted: asd
[asd] --> 1 field extracted: asd
[] --> no field extracted
**/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多