Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. void append_to_file(char *filename, char *line, size_t len)
  2. {
  3. off_t off;
  4. int written, closed;
  5. int fd = open(filename, O_RDWR);
  6. if (fd == -1) {
  7. printf("Error opening file: %s\n", filename);
  8. printf("Error: %s\n", strerror(errno));
  9. return;
  10. }
  11. off = lseek(fd, 0, SEEK_END);
  12. if (off == -1) {
  13. printf("Error seeking file: %s\n", filename);
  14. printf("Error: %s\n", strerror(errno));
  15. close(fd);
  16. return;
  17. }
  18. written = write(fd, line, len);
  19. if (written == -1) {
  20. printf("Error writing to file: %s\n", filename);
  21. printf("Error: %s\n", strerror(errno));
  22. }
  23. closed = close(fd);
  24. if (closed == -1) {
  25. printf("Error closing file: %s\n", filename);
  26. printf("Error: %s\n", strerror(errno));
  27. return;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement