2

I am having trouble with strtok function while opening a csv file with the following data:

999999999,AA999999999,20240121,Uimo,Jacob,19910306,,,,,9999999999,Sam,Gok

I have the following code which uses that function:

char *inFile;
char  line[8192];
int   rowIndex = 0;

if ((inFile = fopen("TestFile.csv", "r")) == NULL)
{
   printf("Could not open input file [%s] \n", filename);
   return 1;
}

// Skip the header line
fgets(line, sizeof(line) - 1, inFile);

while (fgets(line, sizeof(line) - 1, inFile) != NULL)
{  
   char *token = strtok(line, ",");
   int   fieldIndex = 0;

   printf("************** RECORD %d ************** \n", rowIndex);

   while(fieldIndex < 13)
   {
     if (strlen(token) == 0)
     {
        token = ' ';
        printf("Field %d: '%s' \n", fieldIndex, token);
     }
     else
     {
        printf("Field %d: '%s' \n", fieldIndex, token);
     }

      token = strtok(NULL, ",");
      fieldIndex++;
   }   

   rowIndex++;
}

fclose(inFile);

I have the following output and therefore it seems it strtok just bypasses the NULL values separated by commas {,,,,} at the columns 7, 8, 9 and 10:

************** RECORD 0 **************
Field 0: '999999999'
Field 1: 'AA999999999'
Field 2: '20240121'
Field 3: 'Uimo'
Field 4: 'Jacob'
Field 5: '19910306'
Field 6: '9999999999'
Field 7: 'Sam'
Field 8: 'Gok
'
Segmentation fault (core dumped)

I just need the following output:

************** RECORD 0 **************
Field 0: '999999999'
Field 1: 'AA999999999'
Field 2: '20240121'
Field 3: 'Uimo'
Field 4: 'Jacob'
Field 5: '19910306'
Field 6: ' '
Field 7: ' '
Field 8: ' '
Filed 9: ' '
Field 10: '9999999999'
Field 11: 'Sam'
Field 12: 'Gok'
11
  • Unrelated: Why - 1 in sizeof(line) - 1? fgets will not write out of bounds if you just make it sizeof line. Commented Jan 21 at 19:03
  • 2
    This is how strtok works. Read it's man page, it clearly states "From the above description, it follows that a sequence of two or more contiguous delimiter bytes in the parsed string is considered to be a single delimiter: Commented Jan 21 at 19:05
  • 1
    Use strsep to obtain the delimiters between commas. Commented Jan 21 at 20:06
  • 1
    @LuisColorado: strsep is not part of the ISO C standard library. It is a POSIX extension. Note that OP did not tag the question with any particular platform, so OP is not necessarily asking about a POSIX platform. There is nothing wrong with recommending platform-specific functions, but they should generally be marked as such. Commented Jan 21 at 20:13
  • 1
    @LuisColorado, does something seem unreasonable to you about alerting people when you propose a solution revolving around a function that they might not actually have, or that they might be obligated to avoid because they cannot rely on others having it? Commented Jan 21 at 21:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.