I have the following to parse a csv file, and it works fine until i changed
(getfield(tmp, 12)); to
(getfield(tmp, ver)); Is the problem in function declaration ?
here is the code :
const char* getfield(char* line, int num) { const char* tok; for (tok = strtok(line, ";"); tok && *tok; tok = strtok(NULL, ";\n")) { if (!--num) return tok; } return NULL; } int main() { double frame_index[40][300]; int horz; FILE* fp = fopen("output.txt", "r"); char line[1024]; for (int ver; ver<20;ver++) { while (fgets(line, 1024, fp)) { char* tmp = strdup(line); frame_index[ver][horz] = atof(getfield(tmp, ver)); // works if "ver" was explicitly defined printf("AA %f\n", frame_index[ver][horz]); free(tmp); horz++; } rewind(fp); } }
getfield()may returnNULL. Need to test for that before callingatof().