简体   繁体   中英

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' ,

I want save nums to a[] and 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.

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?

If you would like to continue using a scanf approach, I would recommend the negated scanset %[^] format specifier.

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/

From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:

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

I hope this helps!

Let us use the non standard getline , a common library extension. @Abbott to read the entire line into allocated memory.

If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.

Make a function to count and maybe save the numbers. Use strto...() functions for robust parsing.

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. The solution reads an unspecified number of integer values & stores them in 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.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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