Parsing a Text File with Lines of Different Sizes in C -
i have program reads lines text file , puts in list. words on each line categorized command (add or remove element), position, product , price. in order. lines not have price or product, command , position. parse lines correctly first few lines, if text file comes short of few words, example, doesn't tell price or product command , postion, segfault. there way me check if line text file shorter can parse elements there?
the text file looks this:
add 0 staples 2 add 1 paper 4 add 1 tape 3 add 3 paperclips 2 remove 2 remove 0
the function parse follows.
while (fgets (buff, 140, test)) { token = strtok(buff, " "); instructions = malloc(sizeof(char)*strlen(token)+1); strcpy(instructions, token); token = strtok(null, " "); listposition = atoi(token); token = strtok(null, " "); product = malloc(sizeof(char)*strlen(token)+1); strcpy(product,token); token = strtok(null, " "); price = atoi(token);
use delimiter each line. read each line delimiter parse that. can use delimiter after each in input. such command;product;price; #
.
choose delimiter wont show in normal input youre reading in.
Comments
Post a Comment