Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include "holberton.h"
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. /** * read_textfile - function that print a binary
  8. * @filename: name of file
  9. * @letters: letters
  10. * Return: void */
  11. ssize_t read_textfile(const char *filename, size_t letters)
  12. {
  13. int o, r, w, c; char *d;
  14. d = malloc(sizeof(char) * letters);
  15. if (d == NULL)
  16. return (0);
  17. o = open(filename, O_RDWR);
  18. if (o == -1)
  19. {
  20. free(d);
  21. return (0);
  22. }
  23. r = read(o, d, letters);
  24. w = write(STDOUT_FILENO, d, (ssize_t)r);
  25. if (w == -1)
  26. {
  27. free(d);
  28. return (0);
  29. }
  30. if (r != w)
  31. return (0);
  32.  
  33. free(d);
  34. c = close(o);
  35.  
  36. if (c == -1)
  37. return (0);
  38.  
  39. return (w);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement