Im having troubles with the next piece of code...
I have defined a linked list struct
typedef struct node {
char *cmd;
char *value;
int delay;
struct node *next;
} node_t;
im reading commands from a text file called commands.txt
write:0x02:1
read:0x04:2
set:0xff:2
reset:0xfa:2
the code below works if i use two buffers (temp and temp1) but if i replace temp1 with temp it does not parse it correctly. it gives me
Tokenized2 read x04 1
Tokenized2 read 0x04 2
Tokenized2 read x04 1
Tokenized2 read 0x04 2
The code is:
char *filename = "commands.txt";
FILE *fp = fopen(filename, "r");
node_t *head = (node_t *)malloc(sizeof(node_t));
node_t *hp = head;
char temp[14];
char temp1[14];
fgets(temp, 14, fp);
hp->cmd = strtok(temp, ":");
hp->value = strtok(0, ":");
hp->delay = atoi(strtok(0, ":"));
hp->next = (node_t *)malloc(sizeof(node_t));
hp = (node_t *)hp->next; Blaz
fgets(temp1, 14, fp);
hp->cmd = strtok(temp1, ":");
hp->value = strtok(0, ":");
hp->delay = atoi(strtok(0, ":"));
hp->next = NULL;
hp = head;
printf("Tokenized2 %s %s %d\n", hp->cmd, hp->value, hp->delay);
hp = hp->next;
printf("Tokenized2 %s %s %d\n", head->next->cmd, head->next->value, head->next->delay);
printf("Tokenized2 %s %s %d\n", head->cmd, head->value, head->delay);
printf("Tokenized2 %s %s %d\n", head->next->cmd, head->next->value, head->next->delay);
fclose(fp);
The bottom printfs are redundant, I was just testing if it works with classic access to head and via pointer. Eventually I would like to make this piece of code work.
const unsigned MAX_LENGTH = 256;
char cmdbuffer[MAX_LENGTH];
node_t *head = (node_t *)malloc(sizeof(node_t));
node_t *hp = head;
while (fgets(cmdbuffer, MAX_LENGTH, fp)) {
hp->cmd = strtok(cmdbuffer, ":");
hp->value = strtok(0, ":");
hp->delay = atoi(strtok(0, ":"));
hp->next = (node_t *)malloc(sizeof(node_t));
hp = (node_t *)hp->next;
}
hp->next = NULL;
printf("Tokenized %s %s %d\n", head->cmd, head->value, head->delay);
printf("Tokenized2 %s %s %d\n", head->next->cmd, head->next->value, head->next->delay);
How can I solve this problem?