c - Ignore space or newline when reading a line -
i'm reading line till enter/return pressed(like terminal), have problems when comes ignoring space(s) , enter(s).
this how read , check space/new line/comment:
char line[256]; while(printf("%s>", shell_name) && scanf(" %50[^\n]", line) != eof){ if(isspace(*line) == 0 && line[0] != '#' && line[0] != '\n'){
input example:
mysh>echo lol lol mysh> *spaces* mysh> mysh> *next line(enter)* mysh>
the " "
in scanf(" %50[^\n]", line)
consumes leading whites-space (including '\n'
), not leading spaces.
isspace(*line) == 0
, line[0] != '\n'
true.
suggest fgets()/sscanf()
. user input far easier handle first getting line , then 2) parsing it.
char buf[256]; if (fgets(buf, sizeof buf, stdin) == null) handleeof(); char line[256]; if (sscanf(buf, " %50[^\n]", line) < 1) handle_whitespaceonlyline(); goodtogo();
Comments
Post a Comment