Declaring a 2D array and then looping to fill each elements from a file is a horribly fragile way to write an input routine. What happens if there is one float too few? (You invoke Undefined Behavior and all bets are off)
A good approach is a line-oriented approach where you read a row of floats at a time, and then, parse the needed floats from each line with either strtok() (or if empty fields are possible, strsep()), or you can use a pair of pointers and strtof() to work down the line, or with a known fixed number of floats per-line, sscanf() will do. All allow the validation of each float read.
Can you read one float at a time with fscanf()? Sure, you just need to keep separate counters and when a row worth of floats are read, reset your column count and increment the row count. Repeat until your run out of rows, or you file ends. Whichever happens first. One other caveat in reading one float at-a-time is you need some way to validate each row has the correct number of floats. You can do that by setting a separate variable to contain the number of columns in the first row, and then compare the number in each successive row with the first row count.
You could do that as follows:
char c;
int rows = 0, col = 0, cols = 0, rtn;
float dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];
...
while (rows < NUMBEROFINPUT) { /* while less than NUMBEROFINPUT rows */
/* read float saving character that follows and the return */
rtn = fscanf (dataFileptr, "%f%c", &dataFileInput[rows][col], &c);
if (rtn < 1) /* if nothing read or EOF, break */
break;
else if (rtn == 1 || c == '\n') { /* if only float read or next is \n */
col++; /* increment no of col */
rows++; /* increment no. of rows */
if (cols && col != cols) { /* if cols set validate cols no. in col */
fprintf (stderr, "error: unequal number of columns, row: %d\n", rows);
return 1;
}
if (!cols) /* if first row, set no. of cols */
cols = col;
col = 0; /* reset col for next row */
}
else /* both values read and char not '\n' */
col++; /* increment col count */
}
for (int i = 0; i < rows; ++i) /* output results */
for (int j = 0; j < cols; ++j)
printf("a[%d][%d] = %.2f\n", i+1,j+1, dataFileInput[i][j]);
By using the col and rows indexes above, you are keeping track of the state of your read. By returning if (cols && col != cols), after cols is set by the first row number of columns, you force all subsequent rows to contain that number of values ensuring your 2D array is completely filled.
Example Use/Output
Seting dataFileptr to stdin and reading your example input with stdin would result in:
$ ./bin/read2Dcsv <<< -1,0.53,1,1,1,0,0.8,1,0.5,0
a[1][1] = -1.00
a[1][2] = 0.53
a[1][3] = 1.00
a[1][4] = 1.00
a[1][5] = 1.00
a[1][6] = 0.00
a[1][7] = 0.80
a[1][8] = 1.00
a[1][9] = 0.50
a[1][10] = 0.00
There are many ways to do this. A line-oriented approach provides the most flexible and robust approach, but you can make a float at a time approach reasonably flexible as well. Let me know if you have any further questions.
int dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];is enough instead ofint *dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];fscanfstatement skip over the comma seperator between each number in the input?