theres a thread on book recommendations here. there are tutorials and quizzes on cprogramming.com (look at the left menu).
youre right, C can be used to create very fast and efficient programs, if designed well. you might be able to write a faster program using C++ and the datastructures mentioned above, rather than using C and creating your own (and maybe improperly). however, it would be good practice to stick with C, as you seem you want to, so you can get appreciation for C++ and utilizing its OOP.
since you realize that in the future you may want to handle lines each with unique number of numbers on it, it would be good to write your program so it can handle that now, rather than having to redo a lot of things when you decide to change it.
again, i stick with my approach as mentioned above.. but would also appreciate the comments from more proficient programmers.
to count the number of rows in your matrix, read the file character by character. whenever you find a newline ('\n') then increment your counter.
now to count the number of columns on each line, start at the beginning of the file, read each character until you reach a '\n' (ie read each character on a line). any time you read a space ' ', before the '\n' is found, you increment the column count for that row.
Code:
int** matrix; // obviously, our actual matrix of numbers
int rowCount = 0; // obviously, the number of rows in the matrix
int* columns // an array of ints, ie columns[0] will store the number of columns in row 0, etc
// open file
// make a loop, reading the file character by character
// inside the loop, increase rowCount when you find '\n' character
// once done the loop, rewind the file to the beginning
matrix = malloc(rowCount * sizeof(int*));
columns = malloc(rowCount * sizeof(int));
// make a loop, from X = 0 to rowCount
// initialize column[X] to 0
// make an inner loop, reading the file character by character until a '\n' is reached
// inside this inner loop, increase column[X] whenever you see a space ' '
// once the inner loop is done (for this iteration of the outer loop) then do:
matrix[X] = malloc(columns[X] * sizeof(int));
all this would do is get the size of the matrix! however the (i think) hard part would be done, and you would know the number of rows, and the length of each row (columns), even if each row is different length. you still have to then start reading at the beginning of the file, and store each number into its corresponding place in the matrix.
again, im sure theres a better way to do this in C, and hope someone can shed some light.
edit: the above method would get very screwed up if the input file was something like:
Code:
1 2 3 4 (even more spaces here)
therefore it would be better to use a different delimeter (instead of space) or an entirely different way all together. if you can safely assume the input file will be in the exact format you expect, then this method should work.