-
-
Save josephok/c89fb05e202d29e3800f to your computer and use it in GitHub Desktop.
The Linux Programming Interface Exercise 5-2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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