Pregunta

I'm trying to insert int as value to gtree, which comes from glib. I don't how should I assign and insert it. Here you can see what I have tried:

void readFilec(GTree *tree)
{
  FILE *fp = fopen("cfg/file.csv", "rb" );
  char * line = NULL;
  size_t len = 0;
  ssize_t read;

  if (fp == NULL)
  exit(EXIT_FAILURE);
  char *p1;
  int  p2;
  while ((read = getline(&line, &len, fp)) != -1)
  {
    printf("%s", line);
    p1 = strtok(line, "|");
    p2 = (int)atoi(strtok(NULL, "|"));
    g_tree_insert(tree,(gpointer *) g_strdup(p1), (gpointer *)  p2);

    printf("-%s%d ", p1, p2);

  }

  if (line)
  free(line);
  //exit(EXIT_SUCCESS);

}

The file which I read looks like that:

cfg/file.csv
AA1.E|1 
AA2.E|2
¿Fue útil?

Solución

Try to remove the * after gpointer, as it already is a void* and add '&' in front of p2 to get its adress.

g_tree_insert(tree, (gpointer) g_strdup(p1), (gpointer)  &p2);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top