Advertisement
HAR1F

Untitled

May 21st, 2020
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4.  
  5. int main()
  6. {
  7.   int fd;
  8.   const size_t size = 13;
  9.   char str[size + 1] = {0};
  10.  
  11.   if ((fd = open("myfile", O_RDONLY)) < 0)
  12.   {
  13.     printf("Can\'t open file\n");
  14.     exit(-1);
  15.   }
  16.  
  17.   int offset = 0,
  18.       bytes_read;
  19.  
  20.   for (; (bytes_read = read(fd, str + offset, size - offset)) > 0;)
  21.   {
  22.     offset += bytes_read;
  23.   }
  24.  
  25.   printf("%s\n", str);
  26.  
  27.   if (close(fd) < 0)
  28.   {
  29.     printf("Can\'t close file\n");
  30.   }
  31.  
  32.   return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement