1

I'm going back to C at my University a year of Java, and our first assignment is to read the values present in a .CSV file, but the textbook isn't clear and the prof hasn't helped us much. I really don't have a lot of direction on this, and the assignment is due soon, so I really need some direction!

I think I can get through most everything myself, but I'm just not certain what this block of code does...

static int extractItems(char *line, char row[][MAXLEN]) {
    char *item;
    int col = 0;
    for( ; ; ) {
        item = strtok(line, ",\r\n");
        if (item == NULL)
            break;
        if (col >= MAXCOLS) {
            tooWide = 1;
            break;
        }
        strncpy(row[col], item, MAXLEN);
        row[col][MAXLEN] = '\0'; // force null termination
        col++;
        line = NULL;  // required by strtok function
    }
    return col;
}

Col refers to the column number, the first being 0.

I understand that it checks if there is nothing in the line and whether it's too wide, but the rest is foreign to me.

2 Answers2

1

I think the best way to explain is to comment the code:

static int extractItems(char *line, char row[][MAXLEN]) {
                                      //line is a char* to your array of chars
                                                     // - one long string
                                      //row is a two dimensional array of chars
                                                     // - an array of strings
    char *item;
    int col = 0;
    for( ; ; ) {                      //infinite loop, we can exit it with "break" though
        item = strtok(line, ",\r\n"); //string->token.  Returns a pointer to a string,
                                      //which is the next token in the input.
                                      //it does this by replacing the first character found 
                                      //from the second string passed (",\r\n") with '\0'
                                      //and returning the pointer to where it started 
                                      //searching from.
                                      //i.e. it cuts the string up into substings (tokens)
                                      //and returns a pointer to the next one each time 
                                      //it is called.
        if (item == NULL)             //if NULL we are at end of the line. so exit loop
            break;
        if (col >= MAXCOLS) {         //if we have read too much (reached our limit) then exit
            tooWide = 1;              //is this a global? anyway it is used to signal that there was too much data
            break;
        }
        strncpy(row[col], item, MAXLEN); //copy the temporary string returned by strtok to the array
        row[col][MAXLEN] = '\0';      // force null termination (C_string remember?)
        col++;                        // increment the number of words counted
        line = NULL;                  // required by strtok function
                                          //passing in NULL gets strtok to continue scanning
                                          //from the end of the previous successful scan
    }
    return col;
}

for more info on strtok: this answer describes it well

Community
  • 1
  • 1
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
0

Lets break the program and analyse,

static int extractItems(char *line, char row[][MAXLEN]) {

line is an array which contains the current line being read from the CSV file.
row is a two dimentional array where each item is a individual element from the CSV file

 item = strtok(line, ",\r\n");

strtok function splits the string, first argument, here line into tokens based on the deliminter string, second argument, here ,\n\r That is if the CSV file contains a line
Hello, world then subsiquent calls to strtok with the line, produces Hello => split line at , world => split line at \n, \r is used in windows as newline
For further reading refer this link

Now the individual elements are obtained in item which is copied on to the row by the strncpy which copies n, MAXLEN characters.

strncpy(row[col], item, MAXLEN); 
row[col][MAXLEN] = '\0';

It is then deliminted by \0

col++;                       
line = NULL;

incremennts to the next position for next item and sets line to NILL for next.

Since the array row is passed by reference as default, once the program is run the row will contains each of the comma seperated value in the CSV file.

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52