简体   繁体   English

如何从C语言的输入到2个数组中读取两行数字? 不知道数量多少? 在C

[英]How read two lines numbers from input to 2 arrays, C language? Without knowing the amount of number? in C

For example, input is: 例如,输入为:

12 23 32 41 45
22 11 43

lines end with '\\n' , 行以'\\n'结尾,

I want save nums to a[] and b[] ; 我想将数字保存到a[]b[] ;

a[] = {12, 23, 32, 41, 45}
b[] = {22, 11, 43}

The point is I DON'T KNOW how many number of each line. 关键是我不知道每行多少个。

If I know line1 n numbers and line2 m numbers, I will use "for loop", and no question. 如果我知道line1 n数字和line2 m数字,我将使用“ for循环”,这是毫无疑问的。

Just like, 就像,

for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);

But, I DO NOT know n and m, How to do, guys? 但是,我不知道n和m,怎么办,伙计们?

If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier. 如果您想继续使用scanf方法,我建议使用否定的scanset %[^]格式说明符。

scanf("%[^\\n]", pointerToCharArray)

This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). 它应该读取不限数量的字符, 但不包括指定的字符(在我们的示例中为换行符)。 If you'd like to discard the newline, read it in as follows: 如果您想放弃换行符,请按以下方式阅读:

scanf("%[^\\n]\\n", pointerToCharArray)

A link to the reference page can be found below. 可以在下面找到参考页面的链接。 The negated scanset specifier is included in the list: 否定的扫描集说明符包括在列表中:

http://www.cplusplus.com/reference/cstdio/scanf/ http://www.cplusplus.com/reference/cstdio/scanf/

From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. 从这一点出发,使用strtok()将scanf的输出标记为数字数组是一件简单的事情。 If you are not familiar with strtok, another reference link is provided below: 如果您不熟悉strtok,则在下面提供另一个参考链接:

http://www.cplusplus.com/reference/cstring/strtok/ http://www.cplusplus.com/reference/cstring/strtok/

I hope this helps! 我希望这有帮助!

Let us use the non standard getline , a common library extension. 让我们使用非标准的getline ,这是一个常见的库扩展。 @Abbott to read the entire line into allocated memory. @Abbott行读到分配的内存中。

If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11. 如果需要一个数组并在运行时确定其大小,请使用C99和C11(可选)中提供的可变长度数组

Make a function to count and maybe save the numbers. 创建一个函数进行计数并保存数字。 Use strto...() functions for robust parsing. 使用strto...()函数进行健壮的解析。

size_t count_longs(char *line, long *dest) {
  size_t count = 0;
  char *s = line;
  for (;;) {
    char *endptr;
    long num = strtol(s, &endptr, 10);
    if (s == endptr) {
      break;  /// no conversion
    }
    s = endptr;
    if (dest) {
      dest[count] = num;
    }
    count++;
  }
  return count;
} 

Sample code snippet 示例代码段

  char *line = NULL;
  size_t len = 0;
  ssize_t nread = getline(&line, &len, stdin);  // read the whole line
  if (nread != -1) {
    // successful read

    long a[count_longs(line, NULL)];  // determine array size and use a 
    count_longs(line, a);             // 2nd pass: assign array elements. 

    nread = getline(&line, &len, stdin);
    if (nread != -1) {
      // successful read

      long b[count_longs(line, NULL)];
      count_longs(line, b);

      // Do something with a and b
    }
  }
  free(line);
  len = 0;

A solution that could potentially fit what OP asked for would be the code below. 下面的代码可能会满足OP的要求。 The solution reads an unspecified number of integer values & stores them in arr . 该解决方案读取未指定数量的整数并将其存储在arr to break the loop/end input simply input a non-integer value. 要中断循环/结束输入,只需输入一个非整数值即可。 This could be further specified to search for a specific input etc. 可以进一步指定搜索特定的输入等。

int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
    extArr = (int*)realloc(arr, (i+1) * sizeof(int)); 
    arr = extArr;
    arr[i] = c; 

}
free(arr);

I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline or similar) since reallocating memory for every single int value seems redundant to me. 但是,我个人会在我提供的解决方案和armitus答案之间混合使用(如果我不能使用getline或类似方法),因为为每个int值重新分配内存对我来说似乎是多余的。

Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. 编辑由于存在关于我提供的代码以及如何最好地使用它来满足OP的问题的一些问题。 The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free() has to be omitted till one is done with the array(obviously). 代码片段应该展示如何为未指定数量的整数输入创建自己的scanf样式函数,一次读取一个完整的数组(在这种情况下,必须省略free() ,直到对数组进行一次处理(显然)。

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

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