Hello,
I want to read in integers in txt file into 2-D matrix. I found normal way to do this type of job is to 1) get known dimension of the matrix, or 2) define the row-column dimensions bigger enough to hold the possible size of the matrix ahead of time, but I want dynamic allocation of the matrix on the fly.
The reason I want to do this is 1) trying to understand pointer and 2-D array, 2) to remove the restriction of "known" row- or column-numbers ahead of time so that any 2-D array can be read in without pre-defined dimension of the array.
In general my idea is:
1) read in a line by fgets(); increment the row number; realloc() matrix[i];
2) parse the line with strtok(); increment the column number and realloc() the matrix[i][j];
3) read in the individual value to matrix[i][j].
My code was compiled without warning or error, but did not do the thing I expected.
Here is my code:
Code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
//What I am trying to do is scanning from file an integer 2-D array of unknown row/column
/*
matrix_4by3.tab
11 12 13
21 22 23
31 32 33
41 42 43
matrix_4by5.tab
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
http://stackoverflow.com/questions/18943735/read-matrix-in-txt-file-and-store-it-in-array-2d
matrix_mbyn.tab
255 50 9 50 1 50 50 1
50 255 50 50 50 50 50 50
50 50 255 50 50 50 50 50
8 50 50 255 50 50 50 50
50 50 50 50 255 50 50 50
50 50 50 50 50 255 50 50
1 50 50 50 50 50 255 50
2 50 50 50 50 50 50 255
Similarly, I want dynamically allocate memory for rows and columns
char **array = malloc(sizeof(char *) * row) // **array = mat[row][col]
And each row like:
array[r] = malloc(sizeof(char) * col)
*/
size_t chomp(char *);
int main(int argc, char *argv[])
{
int i = 0, j = 0;
FILE *fptr = fopen(argv[1], "r");
char seps[] = " ,;\t";
char line_buffer[1024];
char *token;
int var;
// int matrix[1000][100]; // I want this dynamically allocated as below
/**************************************************************/
int **matrix = malloc(2 * sizeof(int *)); // the matrix is at least two rows, otherwise it is not a 2-D array.
if (!fptr) {
printf("Couldn't open file %p for reading.\n", fptr);
return 1;
}
while (fgets(line_buffer, 1024, stdin) != NULL) {
if (line_buffer[0] == '\n') // skipp blank row
continue;
chomp(line_buffer); //chop newline
matrix[i] = realloc(matrix[i], (i+1) * sizeof(int *)); //increment by row number i, but seems wrong with this line
token = strtok(line_buffer, seps);
while (token != NULL) {
sscanf(token, "%d", &var);
j++;
matrix[i] = realloc(matrix[i], j * sizeof(int *)); //increment by column number j; not sure with this line either.
matrix[i][j] = var;
printf("%d\t", matrix[i][j]);
token = strtok(NULL, seps);
}
printf("\n");
i++;
}
/***************************************************************/
// free(matrix); //but I am not sure this is correct for 2-D array
int count;
for ( count = 0; count < i; count++)
free (matrix[count]);
free (matrix);
return 0;
}
size_t chomp(char *str)
{
size_t len = strlen(str);
while ((len > 0) && isspace(str[len - 1]))
str[--len] = '\0';
return (len);
}
Code:
$ ./a.out < matrix_4by3.tab
And I am expecting:
Code:
11 12 13
21 22 23
31 32 33
41 42 43
Nothing showed up but segmentation fault!
Thanks for any help!