strlen(char *s)

返回字符串不包括"\0"的长度。

下面是解释:

NAME
       strlen - calculate the length of a string

SYNOPSIS
       #include <string.h>

       size_t strlen(const char *s);

DESCRIPTION
       The  strlen()  function  calculates  the  length  of  the string s, not
       including the terminating `\0' character.



在unix中,读入一行时都会在后面加入"\n"紧跟一个"\0",所以strlen往往得不到正确的答案。应该是strlen(s) - 1

所以清除换行符的方法是
char test[MAX_LINE];
//....read from stdin
test[strlen(test) - 1] = '\0';

你可能感兴趣的:(unix)