This uses fgets to read each line and strtol to parse integers from a line.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
int get_int_range ( char *line, char **next, char *term, int *value, int min, int max);
int get_int_series ( int cols, int dest[][cols], int inputrow, int min, int max, char *line, char *delim);
int get_int_count ( int min, int max, char *line, char *delim);
int main( int argc, char *argv[])
{
char line[900] = {'\0'};
char file[100] = {'\0'};
int valid = 0;
int rows = 0;
int cols = 0;
int eachrow = 0;
int eachcol = 0;
FILE *fp = NULL;
printf ( "Enter the name of the file with it's extension\n");
fgets ( file, sizeof ( file), stdin);
file[strcspn ( file, "\n")] = '\0';//remove trailing newline
if ( ( fp = fopen ( file, "r")) != NULL) {
fgets ( line, sizeof ( line), fp);//read a line
rows = get_int_count ( INT_MIN, INT_MAX, line, ",\n");
rewind ( fp);
if ( rows) {
cols = rows;
//once the size is obtained, the array can be declared
int array[rows][cols];
for(eachrow = 0; eachrow < rows; eachrow++) {
if ( ( fgets ( line, sizeof ( line), fp)) == NULL) {//read a line
fclose ( fp);
printf ( "Problem! not enough lines in file\n");
return 1;
}
valid = get_int_series ( cols, array, eachrow, INT_MIN, INT_MAX, line, ", \n");
if ( !valid) {
fclose ( fp);
printf ( "Problem!\n");
return 1;
}
}
if ( ( fgets ( line, sizeof ( line), fp)) != NULL) {//read a line
fclose ( fp);
printf ( "Problem! too many lines in file\n");
return 1;
}
for(eachrow = 0; eachrow < rows; eachrow++) {
for(eachcol = 0; eachcol < cols; eachcol++) {
printf("[%d] ", array[eachrow][eachcol]);
}
printf("\n");
}
printf("\nDone\n");
}
fclose ( fp);
}
return 0;
}
int get_int_range ( char *line, char **next, char *term, int *value, int min, int max)
{
long int input = 0;
char *end = NULL;
errno = 0;
input = strtol ( line, &end, 10);//get the integer from the line
if ( end == line) {
printf ( "input MUST be a number\n");
return 0;
}
if ( *end != '\0' && ( strchr ( term, *end) == NULL)) {
printf ( "problem with input: [%s] \n", line);
return 0;
}
if ( ( errno == ERANGE && ( input == LONG_MAX || input == LONG_MIN))
|| ( errno != 0 && input == 0)){
perror ( "input");
return 0;
}
if ( input < min || input > max) {
printf ( "input out of range %d to %d\n", min, max);
return 0;
}
if ( next != NULL) {
*next = end;
}
*value = input;//set the value
return 1;
}
int get_int_series ( int cols, int dest[][cols], int inputrow, int min, int max, char *line, char *delim)
{
char *end = NULL;
char *each = NULL;
int valid = 0;
int input = 0;
int count = 0;
int temp[cols];
each = line;
do {
valid = get_int_range ( each, &end, delim, &input, INT_MIN, INT_MAX);
if ( !valid) {
printf ( "input MUST be a number\n");
return 0;
}
if ( valid) {
temp[count] = input;
count++;
if ( count > cols) {
printf ( "too many integers. %d entered. only enter %d\n", count, cols);
return 0;
}
}
while ( *end && strchr ( delim, *end)) {//skip any number of delimitors
end++;
}
each = end;
} while ( end && *end);
if ( count < cols) {
printf ( "too few integers. need %d entered. only entered %d\n", cols, count);
return 0;
}
while ( count) {
count--;
dest[inputrow][count] = temp[count];//set the value
}
return 1;
}
int get_int_count ( int min, int max, char *line, char *delim)
{
char *end = NULL;
char *each = NULL;
int valid = 0;
int input = 0;
int count = 0;
each = line;
do {
valid = get_int_range ( each, &end, delim, &input, INT_MIN, INT_MAX);
if ( !valid) {
return count;
}
if ( valid) {
count++;
}
while ( *end && strchr ( delim, *end)) {//skip any number of delimitors
end++;
}
each = end;
} while ( end && *end);
return count;
}