#include #include #include #include #include #define NUM_NAMES 5 #define BLOCK_SIZE 10 int main(int argc, char** argv) { int fd = open("test.txt", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); if (fd < 0) { fprintf(stderr, "return from open %d\n", fd); return 1; } char names[NUM_NAMES][BLOCK_SIZE] = {"Adam", "Bob", "Cathy", "Daniel", "Edward"}; int i; for (i = 0; i < NUM_NAMES; ++i) { /* move to the beginning of block a name will start at the beginning of every 10 bytes */ int r = lseek(fd, BLOCK_SIZE*i, SEEK_SET); if (r < 0) { fprintf(stderr, "return from lseek unexpected -- %d -- %s\n", r, strerror(errno)); return 1; } /* size of string plus a null char */ int len = strlen(names[i]) + 1; r = write(fd, names[i], len); if (r != len) { fprintf(stderr, "return from write unexpected -- %d -- %s\n", r, strerror(errno)); return 1; } } close(fd); return 0; }