#include #include #include #include int main(void) { const char *path = "ostep_ch39_demo.tmp"; int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644); if (fd < 0) { perror("open"); return 1; } const char msg[] = "hello from files\n"; ssize_t n = write(fd, msg, sizeof(msg) - 1); if (n != (ssize_t)(sizeof(msg) - 1)) { perror("write"); close(fd); unlink(path); return 1; } if (lseek(fd, 0, SEEK_SET) < 0) { perror("lseek"); close(fd); unlink(path); return 1; } char buf[32]; ssize_t r = read(fd, buf, sizeof(buf) - 1); if (r < 0) { perror("read"); close(fd); unlink(path); return 1; } buf[r] = '\0'; printf("wrote %zd bytes, read back: %s", n, buf); close(fd); if (unlink(path) != 0) { perror("unlink"); return 1; } printf("unlinked successfully\n"); return 0; }