I wrote below code to read line by line from stdin ex.
city=Boston;city=New York;city=Chicago\n
and then split each line by ';' separator and print of each record. Then, in another loop, I try to split the record into the delimiter '=' to get to the actual values. But for some reason, the main (first) loop does not go beyond the first iteration, why?
char* del1 = ";";
char* del2 = "=";
char input[BUFLEN];
while(fgets(input, BUFLEN, fp)) {
input[strlen(input)-1]='\0';
char* record = strtok(input, &del1);
while(record) {
printf("Record: %s\n",record);
char* field = strtok(record, &del2);
while(field) {
printf("Field: %s\n",field);
field = strtok(NULL, &del2);
}
record = strtok(NULL, &del1);
}
}
source
share