Skip to content

Instantly share code, notes, and snippets.

@josephok
Created January 5, 2015 13:27
Show Gist options
  • Select an option

  • Save josephok/c89fb05e202d29e3800f to your computer and use it in GitHub Desktop.

Select an option

Save josephok/c89fb05e202d29e3800f to your computer and use it in GitHub Desktop.
The Linux Programming Interface Exercise 5-2
Write a program that opens an existing file for writing with the O_APPEND flag, and
then seeks to the beginning of the file before writing some data. Where does the
data appear in the file? Why?
It appears at the end. Cause you set O_APPEND, every write will append at the end of the file.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
int main(int argc, char const *argv[])
{
int fd;
fd = open("test", O_RDWR | O_APPEND);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}
off_t offset;
lseek(fd, 0, SEEK_SET);
write(fd, "9999", 4);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment