I fail parsing a CSV file in C. I need to file a struct with data from that file. This is the relevant part of my structure:
typedef struct Info {
/* Some strings, integers, etc. */
char correct; /* This is the value I can't set */
short int status;
} t_info;
One line from my file looks like this xxxxxx;xxxxxxx;xxxxxxx;D;254 (the D is my problem, see below).
char line[1024]; /* Buffer */
t_info info;
fgets(line, sizeof(line), fp);
strcpy(info.xxxxxx, getLine(line, 1)); /* Works */
strcpy(info.xxxxxx, getLine(line, 2)); /* Works */
strcpy(info.xxxxxx, getLine(line, 3)); /* Works */
strcpy(info.correct, getLine(line, 4)); /* Crashs! */
The getLine() function is taken from this post:
const char *getLine(char *line, int num)
{
const char *tok, *tmp = strdup(line);
for (tok = strtok(tmp, ";"); tok && *tok; tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
What is my problem?