1

I have a CSV of strings that looks like so:

"","Orange","","","","","","Red",""
"Orange","","Blue","","","","","Black",""
"","Blue","","Pink","","Any","","","Any"
"","","Pink","","Green","Red","","",""
"","","","Green","","Blue","","",""
"","","Any","","BLue","","Orange","",""
"","","","Red","","Orange","","Green","Black"
"Red","Black","","","","","Green","","Yellow"
"","","Any","","","","Black","Yellow",""

and I would like to place it into a 2d array of strings (I'll ignore the quotation marks later). I've tried many different ideas but can't seem to get any to work properly.

This code is close but the output is off in a way I can't make sense of. It also parses and tokenizes the file correctly. It seems to go bad when it puts the tokens into the array. Here is the code taken from my program:

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>

 #define VERTICES 9

 int main(void)
 {
     const char *colors[VERTICES][VERTICES];

     char buffer[1024];
     char *record, *line;
     int i = 0;
     int j = 0;
     FILE *fstream = fopen("Colors.dat", "r");
     if (fstream == NULL)
     {
         printf("\n file opening failed\n");
         return -1;
     }
     while ((line = fgets(buffer, sizeof(buffer), fstream)) != NULL)
     {
         record = strtok(line, ",");
         while(record != NULL)
         {
             printf(" %s", record);
             colors[i][j] = record;
             //printf(" %s"), colors[i][j];
             record = strtok(NULL, ",");
             j++;
         }
         j = 0;
         ++i;
     }

     printf("\n============================================\n\n");

     for (i = 0; i < VERTICES; i++)
     {
             for (j = 0; j < VERTICES; j++)
             {
                 printf("%s | ", colors[i][j]);
             }
         printf("\n");
     }
     return 0;
 }

If you uncomment the line in the second nested while loop and comment out the two for loops you get odd output as well. Thanks!

2
  • 1
    Maybe something like colors[i][j] = record; --> colors[i][j] = strdup(record);? colors, as now used, is just fill with pointers into buffer. Commented Jun 9, 2016 at 20:20
  • @chux Wow, that might have done it. Do you want to put that up in an answer? Commented Jun 9, 2016 at 20:24

1 Answer 1

1

OP is simple recording the address of the read buffer, which gets updated on subsequent reads.

Need to allocate/copy the strings for later use.

// colors[i][j] = record;
colors[i][j] = strdup(record);

The remaining colors[i][j] in a line should be set to NULL.

     while(j < VERTICES && record != NULL) {
         printf(" %s", record);
         colors[i][j] = strdup(record);
         assert(colors[i][j]);
         record = strtok(NULL, ",");
         j++;
     }
     while(j < VERTICES) {
         colors[i][j] = NULL;
         j++;
     }

Robust code would also check for allocation failures (assert(colors[i][j]) and would free the memroy when done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.