I've this code in which I receive a server response, check if it's a successful one, and if it is - create the file under the requested path and then write the response body to the file:
I'm testing it with the following URL:
CodePudding user response:
write(fd, buf header_size, tot_read - header_size) != (buf_size - header_size)
From
man 3p write
:Upon successful completion, these functions shall return the number of bytes actually written to the file associated with fildes. This number shall never be greater than nbyte. Otherwise, -1 shall be returned and errno set to indicate the error.
Only when
write() == -1
thenerrno
is set, otherwise it is irrelevant - leftover from some other operation. The number returned bywrite()
can be lower than the requested number of bytes.You have to write a small loop where you loop over the bytes to be written. When by it, consider handling
errno == EAGAIN
.