Checking delimiter on strtok for empty node values

47 views Asked by At

So I have to write a linked list to file and then read the values into a new list, the problem I seem to be having is when I'm using strtok and trying to pass in an empty node such as "". I have delimiters, labelled "z" for testing right now and I know when strtok sees two delimiters next to each other, it completely skips over until it can find another value.

My question would be, is there a way to manually check when two delimiters are next to each other and then add an empty node to my list or is there a much simple solution to this exercise ?

typedef struct NODE {

char * value;
  struct NODE * next;
}
Node;

typedef Node ** List;


int saving_list(List list, char * fileName) {

  FILE * fptr;
  fptr = fopen(fileName, "w");
  char * s = "z";
  //char * t = "3";

  Node * list1 = * list;
  int count = 0;

  if (fptr == NULL) {
    return -1;
  }

  
  while (list1 != NULL) {
   // fputs( list1 -> value, fptr);
    //fputs(s, fptr);
    
    fputs(list1->value, fptr);
    fputs(s, fptr);
    list1 = list1 -> next;
    count++;
  }

 

  fclose(fptr);
  return count;
}


List loading_list(char * fileName) {

  List list = new_list();

  FILE * fptr;
  char buff[255];
  char * s = "z";
  char * token;

  fptr = fopen(fileName, "r");

  if (fptr == NULL) {
    return NULL;
  }

  fgets(buff, 255, (FILE * ) fptr);
  token = strtok(buff, s);
  //token = strsep (&buff, s);

  while (token != NULL) {

    char * result = malloc(sizeof(buff));
    strcpy(result, token);
    push(list,result);
    token = strtok(NULL, s);

      }
  fclose(fptr);
  return list;
}
}
1

There are 1 answers

0
NekoTreci On BEST ANSWER

Instead of a delimiter, you could write the length of value. If you want it to be in human-readable format, then you can use strtol, and you will need a delimiter between the length and the value, as the value may begin with a number possibly.