I'm currently working on a C code program for my research, which involves formation flying of spacecraft. All of my input comes from a text file and I'm trying to clean it up a little. I have to input adjacency matrix for all the spacecraft. AKA I need to input and store NxN matrix, where N in the number of spacecraft. Ex: N = 2 Matrix = 0 1 1 0 Ex: N = 3 Matrix = 0 1 1 1 0 1 1 0 1 Currently I just have to list all the matrix components on a separate line (N=2 means the matrix is on 4 lines of the text file) and this is not very intuitive for larger numbers of spacecraft. Is there a way to use sscanf dynamically to read in N number of integers on a line? So for N = 2 or 3 it would do the equivalent of these N = 2 Code: sscanf(line, "%d %d", array[0][0], array[0][1]) sscanf(nextLine, "%d %d", array[1][0], array[1][1]) N = 3 Code: sscanf(firstLine, "%d %d %d", array[0][0], array[0][1], array[0][2]) sscanf(seondLine, "%d %d %d", array[1][0], array[1][1], array[1][2]) sscanf(thirdLine, "%d %d %d", array[2][0], array[2][1], array[2][2])
While I'm sure there's some fancy foot-work that can be done with the "..." param, maybe you should consider using string tokenizers. You can then feed the numbers in like: Code: num1,num2,num3,num4,num5,num6 Then loop through that, something like: Code: char* cPtr = strtok(inputStr, ","); int i = 0; int arr[50]; while (cPtr) { arr[i] = atoi(cPtr); cPtr = strtok(NULL, ","); ++i; } Where "inputStr" is a char buffer you got via an input, like: Code: char* inputStr[100]; fscan("%s\0", inputStr); Or something to that effect.