Well I read input from user as:
scanf("%[^\n]", message);
And I initialise char message [100] ="";
now, in another function I need to find out length of input in message, I did it easily with strlen()
, unfortunately it doesn't work correctly when I do later in terminal
echo -e "he\0llo" | .asciiart 50
It will read the whole input BUT strlen
will only return length 2.
Is there any other way I could find out length of input ?
By definition strlen stops on the null character
you have to count/read up to EOF and/or the newline rather than counting up to the null character after you read the string
As said in a remark
%n
allows to get the number of read characters, example :Compilations and executions :
As you can see it is not possible to distinguish an empty line and EOF (even looking at errno)
You can also use
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
:but the possible newline is get and counted in that case :