Hi!

I want to read integers from a string of integers and store the individual
integers in an array. The problem is that I don't know how many integers in
the string would be otherwise I would have used sscanf with a format specifier.

For example:

Input:

10 20 12 4 2 1 4 2 12 32
1 4 1 2 4 12 4 1 4 1 5 7 3 4 6 2 3 5 23 52
2 5 7 23 6 23 42

The number of integers in a particular line is not fixed and it will vary
with each line of input.
I need to store the integers of particular line in a different array
for each line.
How do it? I am a beginner programmer. A help would be appreciated.

I would personally just use fgets or fgetc and parse the characters into integer via atoi() if it was a file.

Through fgetc(), when space/'\n' is encountered, stop reading and parse current string, then continue reading again until '\n'/space is encountered. Repeat.

Otherwise, if through console, just keep reading through the input buffer via getchar() until you read '\n'. Then parse string. Generally, when you input just a little too much stuff, stuff gets left behind.

Edited 5 Years Ago by asrockw7: n/a

This is the basic format type of code. (It's actually from another post on this forum). I like using fgets() to put a row of text into a char buffer array - just make sure it's plenty bigger than the longest possible row of input. Waste a little memory here, to keep everything working right.

I initialized the buffer here with digits, but you will use fgets() to put them into the buffer:

fgets(charArrayName, sizeof(ArrayName), filePointerName);

To read all rows in a data file, use:
while((fgets(charArrayName, sizeof(ArrayName), filePointerName))!= NULL) {
   //your code in here to handle the data
   //this reads all rows in the file, if the file is 
   //opened for reading

}

This is the program showing a few key idea's:

#include <stdio.h>

int main() { //
   int i,j,k,ok,n;
   int c2i[6][7];
   
   char buffer[128]={"0 0 1 0 1 1 0 0 0 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 1 0 1 1 0\n"};
   k=ok=0;
   for(i=0;i<6;i++) {                  //for every row
      for(j=0;j<7;j++) {               //for every column
         ok=sscanf(buffer+k, "%d",&n); //scan a char into n
         if(ok) {                      //we got one
            c2i[i][j] = n;             //into the integer array
            //printf("i: %d  k: %d, %d",i, k,c2i[i][j]); getchar();
            k+=2;                      //and increment an offset k
         }                             //for the next place to look for   
      }                                //the next digit.
   }
   //print each one, for an easy comparison check
   printf(" %s\n",buffer);
   for(i=0;i<6;i++)
      for(j=0;j<7;j++) 
         printf("%2d",c2i[i][j]);

   printf("\n");
   return 0;
}

Instead of the two for loops shown above, you'll want the while loop, I believe.

Edited 5 Years Ago by Adak: n/a

The problem is that I don't know how many integers in the string would be otherwise I would have used sscanf with a format specifier.

strtok() would be the easiest option, but you also need to keep in mind having sufficient memory to hold the integers. If the amount is unknown then you need some way of dynamically growing an array or list. For example:

#include <stdlib.h>
#include <string.h>

char *copy_string(const char *s)
{
    char *copy = malloc(strlen(s) + 1);
    
    if (copy != NULL)
        strcpy(copy, s);
        
    return copy;
}

int *isplit(const char *s, int *size, const char *delim)
{
    char *copy = copy_string(s);
    int *result = NULL;
    
    *size = 0;
    
    for (char *tok = strtok(copy, delim); 
         tok != NULL; 
         tok = strtok(NULL, delim))
    {
        // Omitting error handling for brevity
        ++(*size);
        result = realloc(result, *size * sizeof *result);
        result[*size - 1] = atoi(tok);
    }
    
    free(copy);
    
    return result;
}

#include <stdio.h>

int main(void)
{
    const char *src = "10 20 12 4 2 1 4 2 12 32";
    int size;
    int *nums = isplit(src, &size, " ");
    
    for (size_t i = 0; i < size; i++)
        printf("%d\n", nums[i]);
        
    free(nums);
    
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.