I am using fgets()
for this. However, it seems that there are leftovers at the current loop, from the previous loop. But I declare input
inside the loop, so isn't that a new array at every loop?
int nn=0;
while (1) {
printf("%d\n", nn);
if(++nn==3)break;
char input[101];
char* pch;
printf("|%s|\n", input);
fgets(input, 101, stdin);
input[strlen(input) - 1] = '\0';
printf("|%s|\n", input);
pch = strtok (input," ");
if(!strcmp(pch, "l")) {
pch = strtok (NULL, " ");
readFile(pch, rec, N, buckets, towns);
}
else if(!strcmp(pch, "e")) {
printf("Exiting...\n");
return;
}
else {
printf("Unknown command. Exiting...\n");
return;
}
}
Output:
0
||
l t200.bin
|l t200.bin|
Records found in file 200
1
|l|
l t200.bin
|l t200.bin|
Records found in file 200
2
I am worried that the new command to serve will be tangled up with the previous one, especially if leftovers are kept in input
.