Parse .csv into 3d array in C -


i trying write program permute columns of given csv. aim parse csv 3d char array, transpose it, permute it, transpose back, write out file.

i have issue function getrows(). each time increment next row, seems first row being overwritten. have posted whole thing since not unlikely i've cocked @ earlier stage , causing problem (forgive me if i've done stupid, new c)

#include <stdio.h> #include <stdlib.h>      #define maxlen 10      char*** getrows(file *file, char*** rows);     char*** createarray(int size);          /* fucntion read csv , output rows permuted */     int main()      {         char*** rows;         file *fp;          fp = fopen("text.csv", "r");         if(fp == null)          {             printf("didn't open\n");             return -1;         }         rows = createarray(maxlen);         rows = getrows(fp, rows);         fclose(fp);          return 0;     }  /* initializes array */     char*** createarray(int size)     {         char* values = calloc(size*size, sizeof(char));         char** row;         char*** rows = malloc(size*sizeof(char**));         int i, j;         (i=0; i<size; ++i)         {             row = malloc(size*sizeof(char*));              for(j=0; j<size; ++j)             {                 row[j] = values + j*size;             }          rows[i] = row;     }     return rows; }  /* poppulates array file data */ char*** getrows(file *file, char*** rows) {     int c;     int i, j, k;      = j = k = 0;     while((c=fgetc(file)) != eof)     {         if (c == ' ');         else if (c == ',') /*indicate end of entry , move next entry in row*/         {             rows[i][j][k] = '\0';              ++j;             k = 0;         }         else if (c == '\n') /*indicate end of entry, indicate end of row, move next row*/         {             rows[i][j][k] = '\0';             rows[i][j+1][0] = '\0';             ++i;             j = k = 0;         }         else /* add char entry */         {             rows[i][j][k] = c;              /* debugging */             printf("%i\t%i\t%i\t", i, j, k);             putchar(rows[i][j][k]);             printf("\t");             putchar(rows[0][0][0]);             printf("\n");              ++k;         }     }     rows[i][j][k] = '\0';     rows[i+1][0][0] = '\0';     return rows;      } 

so debugging logs following [i, j, k, rows[i][j][k], rows[0][0][0]]

0       0       0       h       h 0       0       1       e       h 0       0       2             h 0       0       3       d       h 0       0       4       e       h 0       0       5       r       h 0       0       6       1       h 0       1       0       h       h 0       1       1       e       h 0       1       2             h 0       1       3       d       h 0       1       4       e       h 0       1       5       r       h 0       1       6       2       h 0       2       0       h       h 0       2       1       e       h 0       2       2             h 0       2       3       d       h 0       2       4       e       h 0       2       5       r       h 0       2       6       3       h 1       0       0       1       1 1       1       0       2       1 1       2       0       3       1 2       0       0       4       4 2       1       0       5       4 2       2       0       6       4 3       0       0       7       7 3       1       0       8       7 3       2       0       9       7 

i hope that's clear. advice!

your problem in create.

rows points array of rows. each row has columns , each column has value (one value).

when assign address of values column of row, see i not involved. indicates assign addresses of first rows of values repeatedly each row[i][k].

so not first row overwritten, rows overwritten (there 1 row of values).

later, in getrows see use rows rows[i][j][k]. each row[i][j] has size values. there size*size*size values (you allocate size*size):

char*** createarray(int size) {     char* values = calloc(size*size*size, sizeof(char));     char** row;     char*** rows = malloc(size*sizeof(char**));     int i, j;     (i=0; i<size; ++i)     {         row = malloc(size*sizeof(char*));          for(j=0; j<size; ++j)         {             row[j] = values + i*size*size + j*size;         }         rows[i] = row;     }     return rows; } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -