-4

I need to fill a 2d array with numbers from a string so I am using strtok and fgets to get the string and tokenize it. However if I enter "1 2 3 4 5" and the 2d array's dimensions are 2x2, the 5 never gets assigned to number. I want to check if there are more numbers than the matrix can hold but number always ends up being NULL after adding the 4 to the matrix instead of 5. I know SIZE is correct because if I print out stringInputted before strtok it prints out the correct output.

scanf("%d", &rows);
scanf("%d", &columns); 
//SIZE = 2*rows*columns+1
//rows and columns are user inputted and stored using scanf
fgets(stringInputted, SIZE, stdin);
 char *number = strtok(stringInputted, " \n");

 for(i = 0; i < rows; i++){
    for(j = 0; j < columns; j++){
        if(number != NULL)
            matrix[i][j] = atoi(number);
        else{
            printf("ERROR Insufficient numbers entered\n");
            return 0;
        }
        number = strtok(NULL, " \n");
    }
 }
 if(number != NULL) printf("TOO MANY NUMBERS\n");
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Tirth Rami
  • 271
  • 4
  • 15
  • Could you include your definitions of `SIZE`, `rows`, and `columns`? Also, your question mentions `input` but your code lists `stringInputted`. – chrisaycock Jan 26 '17 at 20:19
  • Edited. @chrisaycock – Tirth Rami Jan 26 '17 at 20:24
  • Cannot reproduce when wrapped in a `main` function. For a 2x2 matrix 3 values give "ERROR insufficent..." and 5 values give "TOO MANY NUMBERS" and with 4 values it stays quiet. – Weather Vane Jan 26 '17 at 20:28
  • Hmm thats strange, How are you inputting the values? I have it so the user inputs them in 1 string "2 2 1 2 3 4..." The first two would be the size of the matrix. @WeatherVane – Tirth Rami Jan 26 '17 at 20:29
  • 2
    length of `1 2 3 4 5` is `9`. So `SIZE` needs 10 or more. – BLUEPIXY Jan 26 '17 at 20:30
  • I input the values from a redirected text file containing the 5 values you show. Why would the 5th value `5` ever be assigned to `number` when the loop is 2 * 2 nested iterations? BTW my input string was a generous `char stringInputted[100];` (before you posted the `SIZE` calculation). Never be mean with your string space unless you are in a very limited environment. – Weather Vane Jan 26 '17 at 20:30

1 Answers1

1

Your SIZE is incorrect.

fgets() reads in at most one less than size characters from stream... A terminating null byte ('\0') is stored after the last character in the buffer.

So you need to consume 10 bytes, not nine.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 1
    How would I allocate SIZE if I do not know how many numbers the user will input? Like if they want a 2x2 matrix but they input 10 numbers (so 20 characters including spaces) – Tirth Rami Jan 26 '17 at 20:35
  • @TirthRami And this is the trouble with reading user strings in C. Other languages automatically grow the buffer. In C, you'll have to use dynamic memory allocation and `realloc()` the array if you notice more input available. This is a complicated topic and far beyond the scope of a single comment. – chrisaycock Jan 26 '17 at 20:38
  • @TirthRami why would you need to read numbers that you cannot process? If there can be 12 characters in a signed `int` including the space, the maximum string length will be `rows * columns * 12 + 2` (newline and nul). Add another `12` so that you can know if too many numbers were input. – Weather Vane Jan 26 '17 at 20:38