Advertisement
drankinatty

C read integers from file (jagged array)

May 20th, 2015
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MROWS 100
  5. #define MCOLS 20
  6.  
  7. int main (int argc, char **argv) {
  8.  
  9.     if (argc < 2) {
  10.         fprintf (stderr, "error: insufficient input.  usage: %s filename\n", argv[0]);
  11.         return 1;
  12.     }
  13.  
  14.     FILE *fp = fopen (argv[1], "r");
  15.     if (!fp) {
  16.         fprintf (stderr, "error: file open failed for '%s'.\n", argv[1]);
  17.         return 1;
  18.     }
  19.  
  20.     char *line = NULL;          /* NULL forces getline to allocate  */
  21.     size_t n = 0;               /* max chars to read (0 - no limit) */
  22.     ssize_t nchr = 0;           /* number of chars actually read    */
  23.     int **array = NULL;         /* array of ptrs to array of int    */
  24.     size_t ridx = 0;            /* row index value                  */
  25.     size_t cidx = 0;            /* col index value                  */
  26.     char *endptr = NULL;        /* endptr to use with strtol        */
  27.  
  28.     /* allocate MROWS (100) pointers to array of int */
  29.     if (!(array = calloc (MROWS, sizeof *array))) {
  30.         fprintf (stderr, "error: array allocation failed\n");
  31.         return 1;
  32.     }
  33.  
  34.     /* read each line in file */
  35.     while ((nchr = getline (&line, &n, fp)) != -1)
  36.     {
  37.         /* strip newline or carriage return (not req'd) */
  38.         while (line[nchr-1] == '\r' || line[nchr-1] == '\n')
  39.             line[--nchr] = 0;
  40.  
  41.         if (!nchr)      /* if line is blank, skip */
  42.             continue;
  43.  
  44.         /* allocate MCOLS (20) ints for array[ridx] */
  45.         if (!(array[ridx] = calloc (MCOLS, sizeof **array))) {
  46.             fprintf (stderr, "error: array[%zd] allocation failed\n", ridx);
  47.             return 1;
  48.         }
  49.  
  50.         cidx = 0;       /* reset cidx               */
  51.         char *p = line; /* assign pointer to line   */
  52.  
  53.         /* parse each int in line into array    */
  54.         while ((array[ridx][cidx] = (int)strtol (p, &endptr, 10)) && p != endptr)
  55.         {
  56.             /* checks for underflow/overflow omitted */
  57.  
  58.             p = endptr; /* increment p      */
  59.             cidx++;     /* increment cidx   */
  60.             /* test cidx = MCOLS & realloc here */
  61.         }
  62.         ridx++;         /* increment ridx   */
  63.  
  64.         /* test for ridx = MROWS & realloc here */
  65.     }
  66.  
  67.     /* free memory and close input file */
  68.     if (line) free (line);
  69.     if (fp) fclose (fp);
  70.  
  71.     printf ("\nArray:\n\n  number of rows with data: %zd\n\n", ridx);
  72.  
  73.     /* reset ridx, output array values */
  74.     ridx = 0;
  75.     while (array[ridx])
  76.     {
  77.         cidx = 0;
  78.         while (array[ridx][cidx])
  79.         {
  80.             printf ("  array[%zd][%zd] = %d\n", ridx, cidx, array[ridx][cidx]);
  81.             cidx++;
  82.         }
  83.         ridx++;
  84.         printf ("\n");
  85.     }
  86.  
  87.     /* free allocated memory */
  88.     ridx = 0;
  89.     while (array[ridx])
  90.     {
  91.         free (array[ridx]);
  92.         ridx++;
  93.     }
  94.     if (array) free (array);
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement