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'
- 1insizeof(line) - 1?fgetswill not write out of bounds if you just make itsizeof line.strtokworks. Read it'smanpage, 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:strsepto obtain the delimiters between commas.strsepis 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.