edoardo
edoardo

Reputation: 35

isspace() function return true even if the string doesn't contains any white-spaces character

I'm dealing with a client server application that work as follow:

1) Client read a string from stdin and send it to Server 2) Server check if string contains white-space, if not, then do something(not relevant), otherwise do something else(not relevant).

//i tested this function in other context and it works well 
int checkStr(char *x) {

    while(*x != '\0') {

        if(isspace(*x)) {

            return 1;
        }
    x++;

    }
    return 0;
}

The issue come when the function check a string from this:

void read_cmd_line(char *buf) {
    printf("> ");
    fflush(stdout);
    memset(buf, '\0', BUFSIZE);
    if (read(STDIN_FILENO, buf, BUFSIZE) == -1) {
        perror("reading from stdin:");
        exit(EXIT_FAILURE);
    }
}

Upvotes: 1

Views: 191

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222323

The string read(STDIN_FILENO, buf, BUFSIZE) reads includes a new-line character at the end, and that is a white space character, so isspace correctly returns true (1).

Upvotes: 1

Related Questions