There is nothing difficult about reading an unknown number of rows
and unknown number of columns
from a file in C, but you must pay particular attention to how you do it. While you limit the array to a square (NxN
) array, there is no reason every row can't have a different number of columns (it's called a jagged-array).
Your basic approach is to allocate memory for an array of pointers to type int
for some reasonable anticipated number of rows. You will then read each line. For every line you read you then allocate a block of memory for an array of 'int' for some reasonably anticipated number of integers.
You then convert each string of digits encountered to an integer value and store the number at array[row][col]
. (we actually start storing values at col = 1
and save col = 0
to hold the final number of cols for that row) You keep track of the number of integers you have added to the array and if your number of columns reaches the number you allocated, you then realloc
that array for that row (e.g. array[row]
) to hold additional integers.
You continue reading lines until you have read all the lines. If you reach your original limit on the number of rows, you simply realloc
the array of pointers (e.g. array
) much like you did when you reached your limit with cols
.
You now have all your data stored and can do with it what you will. When you are done, do not forget to free
all memory you have allocated. I have put together a small example to illustrate. This example doesn't require a square matrix and will happily store any number of integers per row. Instead of including the file open/file read code, I simply have the example read data from stdin
.
In order to keep the body of the code clean so it is readable, the conversion, allocation and reallocation code is include in functions (xstrtol
, xcalloc
, xrealloc_sp
(single-pointer) and xrealloc_dp
(double-pointer) ). Let me know if you have questions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#define ROWS 100
#define COLS 16
#define MAXC 256
long xstrtol (char *p, char **ep, int base);
void *xcalloc (size_t n, size_t s);
void *xrealloc_sp (void *p, size_t sz, size_t *n);
void *xrealloc_dp (void **p, size_t *n);
int main (void) {
char line[MAXC] = {0}; /* line buffer for fgets */
char *p, *ep; /* pointers for strtol */
int **array = NULL; /* array of values */
size_t row = 0, col = 0, nrows = 0; /* indexes, number of rows */
size_t rmax = ROWS, cmax = COLS; /* row/col allocation size */
/* allocate ROWS number of pointers to array of int */
array = xcalloc (ROWS, sizeof *array);
/* read each line in file */
while (fgets(line, MAXC, stdin))
{
p = ep = line; /* initize pointer/end pointer */
col = 1; /* start col at 1, num cols stored at 0 */
cmax = COLS; /* reset cmax for each row */
/* allocate COLS number of int for each row */
array[row] = xcalloc (COLS, sizeof **array);
/* convert each string of digits to number */
while (errno == 0)
{
array[row][col++] = (int)xstrtol (p, &ep, 10);
if (col == cmax) /* if cmax reached, realloc array[row] */
array[row] = xrealloc_sp (array[row], sizeof *array[row], &cmax);
/* skip delimiters/move pointer to next digit */
while (*ep && *ep != '-' && (*ep < '0' || *ep > '9')) ep++;
if (*ep)
p = ep;
else /* break if end of string */
break;
}
array[row++][0] = col; /* store ncols in array[row][0] */
/* realloc rows if needed */
if (row == rmax) array = xrealloc_dp ((void **)array, &rmax);
}
nrows = row; /* set nrows to final number of rows */
printf ("\n the simulated 2D array elements are:\n\n");
for (row = 0; row < nrows; row++) {
for (col = 1; col < (size_t)array[row][0]; col++)
printf (" %4d", array[row][col]);
putchar ('\n');
}
putchar ('\n');
/* free all allocated memory */
for (row = 0; row < nrows; row++)
free (array[row]);
free (array);
return 0;
}
/** a simple strtol implementation with error checking.
* any failed conversion will cause program exit. Adjust
* response to failed conversion as required.
*/
long xstrtol (char *p, char **ep, int base)
{
errno = 0;
long tmp = strtol (p, ep, base);
/* Check for various possible errors */
if ((errno == ERANGE && (tmp == LONG_MIN || tmp == LONG_MAX)) ||
(errno != 0 && tmp == 0)) {
perror ("strtol");
exit (EXIT_FAILURE);
}
if (*ep == p) {
fprintf (stderr, "No digits were found\n");
exit (EXIT_FAILURE);
}
return tmp;
}
/** xcalloc allocates memory using calloc and validates the return.
* xcalloc allocates memory and reports an error if the value is
* null, returning a memory address only if the value is nonzero
* freeing the caller of validating within the body of code.
*/
void *xcalloc (size_t n, size_t s)
{
register void *memptr = calloc (n, s);
if (memptr == 0)
{
fprintf (stderr, "%s() error: virtual memory exhausted.\n", __func__);
exit (EXIT_FAILURE);
}
return memptr;
}
/** reallocate array of type size 'sz', to 2 * 'n'.
* accepts any pointer p, with current allocation 'n',
* with the type size 'sz' and reallocates memory to
* 2 * 'n', updating the value of 'n' and returning a
* pointer to the newly allocated block of memory on
* success, exits otherwise. all new memory is
* initialized to '0' with memset.
*/
void *xrealloc_sp (void *p, size_t sz, size_t *n)
{
void *tmp = realloc (p, 2 * *n * sz);
#ifdef DEBUG
printf ("\n reallocating %zu to %zu\n", *n, *n * 2);
#endif
if (!tmp) {
fprintf (stderr, "%s() error: virtual memory exhausted.\n", __func__);
exit (EXIT_FAILURE);
}
p = tmp;
memset (p + *n * sz, 0, *n * sz); /* zero new memory */
*n *= 2;
return p;
}
/** reallocate memory for array of pointers to 2 * 'n'.
* accepts any pointer 'p', with current allocation of,
* 'n' pointers and reallocates to 2 * 'n' pointers
* intializing the new pointers to NULL and returning
* a pointer to the newly allocated block of memory on
* success, exits otherwise.
*/
void *xrealloc_dp (void **p, size_t *n)
{
void *tmp = realloc (p, 2 * *n * sizeof tmp);
#ifdef DEBUG
printf ("\n reallocating %zu to %zu\n", *n, *n * 2);
#endif
if (!tmp) {
fprintf (stderr, "%s() error: virtual memory exhausted.\n", __func__);
exit (EXIT_FAILURE);
}
p = tmp;
memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
*n *= 2;
return p;
}
Compile
gcc -Wall -Wextra -Ofast -o bin/array_ukn_size array_ukn_size.c
Input
$ cat array.txt
0 3 -1 -1 -1 2
3 0 5 -1 -1 -1
-1 -1 0 2 -1 -1
-1 -1 -1 0 2 3
-1 1 -1 -1 0 -1
4 -1 -1 8 1 0
Use/Output
$ ./bin/array_ukn_size <array.txt
the simulated 2D array elements are:
0 3 -1 -1 -1 2
3 0 5 -1 -1 -1
-1 -1 0 2 -1 -1
-1 -1 -1 0 2 3
-1 1 -1 -1 0 -1
4 -1 -1 8 1 0
Memory Check
In any code your write that dynamically allocates memory, it is imperative that you use a memory error checking program. For Linux valgrind is the normal choice. There are so many subtle ways to misuse a block of memory that can cause real problems, the is no excuse not to do it. There are similar memory checkers for every platform. They are simple to use. Just run your program through it.
$ valgrind ./bin/array_ukn_size <array.txt
==14043== Memcheck, a memory error detector
==14043== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==14043== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==14043== Command: ./bin/array_ukn_size
==14043==
the simulated 2D array elements are:
0 3 -1 -1 -1 2
<snip>
==14043==
==14043== HEAP SUMMARY:
==14043== in use at exit: 0 bytes in 0 blocks
==14043== total heap usage: 7 allocs, 7 frees, 1,184 bytes allocated
==14043==
==14043== All heap blocks were freed -- no leaks are possible
==14043==
==14043== For counts of detected and suppressed errors, rerun with: -v
==14043== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)