0

I have a text file which looks something like:

pickupInterval 30.0
.
.
map
 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

and I cannot figure out a way to read the matrix (unknown size but is NxN). This is my code so far:

//other irrelevant code here
while (fgets(buffer, sizeof buffer, fp) != NULL)
{
 sscanf(buffer, "%s", var);
 .
 .
 //looking for known keywords
 .
 .
 if (strcmp(var,"pickupInterval") == 0)
 {
   sscanf(buffer, "%s %f",var,&pickupInterval);
 }
 .
 .
 .
 if (strcmp(var,"map") == 0)
 {
  /* map keyword detected
   * read next line
   * find the size of N (ie. NxN matrix)
   * read next N-1 lines and make matrix out of them (I know how to loop, I don't know how to "read next N-1 lines" part would look like)
   */
 }
}
//other irrelevant code here

It goes line by line, if a keyword is detected, it assigns the value to a global variable. If keyword MAP, however, is detected, I don't know how to proceed.

One idea would be to simply concat all N-1 lines (+ line after Map keyword) after N is found, then make matrix out of it. But again, I'm stuck at the stage of "read next N-1 lines" part, or even the "read line after map found" part.

emihir0
  • 1,200
  • 3
  • 16
  • 39
  • 2
    After you find the keyword `map` you could use a `strtok` loop on the following line to determine how many elements the matrix has. And don't use `strcmp(var,"pickupInterval")==0` to detect that keyword, use `strstr(var,"pickupInterval")==var` because the line holds more than just the keyword. – Weather Vane Oct 09 '15 at 19:15
  • The code would be easier to write if the `map` keyword was followed by the matrix size, e.g. `map 6 6`. Are you allowed to change the file format? – user3386109 Oct 09 '15 at 19:20
  • @WeatherVane the problem is that I don't know how to go the next line (ie. next buffer). And notes, `strstr` makes way more sense, thanks! – emihir0 Oct 09 '15 at 19:26
  • @emihir0 you go to the next line with another `fgets()`. – Weather Vane Oct 09 '15 at 19:27
  • @user3386109 I am not allowed to change the input format, no. – emihir0 Oct 09 '15 at 19:27
  • `strstr` is a little risky, since it would accept "map", "mapper", and "somemap" as all matching the keyword "map". You're better off extracting the keyword with `sscanf` or `strtok` and then do a `strcmp`. – user3386109 Oct 09 '15 at 19:28
  • @user3386109 my advice was for `"pickupInterval"`, but `"somemap"` would not be caught anyway. If `"pickupInterval"` was a subset of other keywords you could add a space such as `"pickupInterval "` – Weather Vane Oct 09 '15 at 19:29
  • @WeatherVane I'm not on my work PC any longer at this moment and so have no way to test it. How would I go about getting the actual string of the line? `buffer = fgets();`? I'm very new to C and hence pointers in general and so I assume it should be more along lines of `strcpy(buffer, fgets());`, altough that doesn't make much sense, considering `fgets()` surely only goes to next line, but is not actual next line I assume? – emihir0 Oct 09 '15 at 19:29
  • @emihir0 no, `fgets()` reads into the buffer. You don't have to copy it. And wait until you are at work and look up some examples of `strtok()`. – Weather Vane Oct 09 '15 at 19:34
  • @user3386109 I just commented - add a space to differentiate substrings. – Weather Vane Oct 09 '15 at 19:35
  • @WeatherVane so `fgets();` would change the line at which `buffer` was pointing? Ie. sequence of lines `printf("%s\n",buffer);` `fgets();` `printf("%s\n",buffer);` would result in `map` `0 3 -1 -1 -1 2`? – emihir0 Oct 09 '15 at 19:40

3 Answers3

2

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)
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

i'd suggest, Read the whole line, then use strtok() to split the line into it's elements (Width is achieved)

e.g. at this question Split string in C every white space

then, when you know how to read a line, read lines, until you find a line that does not start with a number... (Height is achieved....)

Community
  • 1
  • 1
Tomer W
  • 3,395
  • 2
  • 29
  • 44
  • How would one go about making sure the correct elements are obtained (ie. sometimes there are 3 white spaces between elements, sometimes 0 `whitespaces` but a `tab` instead). From what I understand `strok()` simply splits the element based on a token (ie. `' '`). – emihir0 Oct 09 '15 at 19:42
  • use delim to include all the possible delimiters, `strtok(token, " ,-\t");` – Tomer W Oct 09 '15 at 19:55
0

You need something like this in your if section:

#include <stdlib.h>

int store[6][6], row, column;
char *pch;

if( strncmp(buffer, "map", 3) == 0 ) {
    row = 0;
    column = row + 1; // just to enter the while loop the first time
    while(fgets(buffer, 100, fp) != NULL && row < column) {
        pch = strtok(buffer, " ");
        column = 0;
        while (pch != NULL) {
            store[row][column++] = strtol(pch, NULL, 10);
            pch = strtok(NULL, " ");
        }
        row++;
    }
}
jayant
  • 2,349
  • 1
  • 19
  • 27