-
Char array and string
Hi,
I'm using the follow code in a socket connection function:
Code:
char input[100];
ssize_t n;
while((n = read(sockfd, input, sizeof input)) > 0) {
input[n] = '\0';
}
How can I make "input" a dynamic length array? Can't get anything to work.
Thanks for the help.
-
You can't pass a "variable length" array to read(), and expect it to expand automatically to whatever size the input data is.
You have to read the input in blocks of known size. What you do after that is up to you.
Oh, and you need to subtract 1 from the size you pass to read(), so that you have room to append a \0.