I have got my strtok working, but now because I am using scanf which ends the string with NULL char my program is only grabbing one line from redirected standard in. eg.
scanf("%s" , input);
token = strtok (input, ", \n");
while(token != NULL){
i += (strtol(token,0,0));
token = strtok(NULL, ", \n");
}
So I am trying to use functions to check if the program has reached just a NULL of a EOF (from what i understand EOF = -1 NULL = 0) but I’m having trouble getting the pointers and functions to work in my code. P.S. I know it’s crappy code, but it’s late and I’m still working on it while I wait for suggestions to point out my obvious errors that I’m missing.
int scanner();
int check_EOF();
int next_line(char *token);
int main(){
char *token, input[256];
int i = 0;
scanf("%s" , input);
token = strtok (input, ", \n");
if (!scanner())
check_EOF();
printf("%d\n", i);
return 0;
}
int check_EOF(){
while(token !=EOF);
next_line();
}
int next_line(){
while(token != NULL){
return 1;
else return 0;
}
int scanner(){
while(next_line()){
i += (strtol(token,0,0));
token = strtok(NULL, ",")
return 0
}
Is there an easier way that I don’t know about or am I heading in the right direction with lots of errors?
Thanks, Lachlan
From
man scanf:You just need to check the return value of
scanf, notstrtok. Something like: