我用下面的代碼來解析這個CSVCSV解析 - 第二場返回空值
me;val1;val2;val3;val4;val5;
me;val1;val2;val3;val4;val5;
void readcsv()
{
FILE* stream = fopen("input.csv", "r");
char line[1024];
while (fgets(line, 1024, stream))
{
char* tmp = strdup(line);
// printf("Field 1 would be %s\n", getcsvfield(tmp, 1));
printf("Field 1 would be %s\n", getcsvfield(tmp, 1));
printf("Field 2 would be %s\n", getcsvfield(tmp, 2));
// NOTE strtok clobbers tmp
free(tmp);
}
}
//Used for parsing CSV
const char* getcsvfield(char* line, int num)
{
const char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
,但我一直在第二場
Output:
Field 1 would be me
Field 2 would be (null)
Field 1 would be me
Field 2 would be (null)
越來越NULL值我即將做錯了什麼?
是不是像'strtok()'修改輸入? –
@SouravGhosh似乎它的問題。 – techno