Skip to content

Instantly share code, notes, and snippets.

@hfm
Created December 16, 2013 00:41
Show Gist options
  • Select an option

  • Save hfm/7980643 to your computer and use it in GitHub Desktop.

Select an option

Save hfm/7980643 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
int main()
{
struct iovec iov[3];
ssize_t nr;
int fd, i;
char *buf[] = {
"hello world.\n",
"alice in wonderland.\n"
"twitter.\n"
};
fd = open("sample_writev.txt", O_WRONLY | O_CREAT | O_TRUNC);
if (fd == -1) {
perror("open");
return 1;
}
for (i = 0; i < 3; i++) {
iov[i].iov_base = buf[i];
iov[i].iov_len = strlen(buf[i]);
}
nr = writev(fd, iov, 3);
if (nr == -1) {
perror("writev");
return 1;
}
printf("wrote %zd bytes\n", nr);
if (close(fd)) {
perror("close");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment