simple_reader.c 481 B

123456789101112131415161718192021222324
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. int main(int argc, char** argv){
  6. int fd;
  7. char *filename = "/tmp/file";
  8. fd = open(filename, O_RDONLY);
  9. if(fd == -1){
  10. fprintf(stderr, "Cannot open /tmp/file.\n");
  11. exit(1);
  12. }
  13. char buf[20];
  14. size_t nbytes = sizeof(buf);
  15. size_t bytes_read = read(fd, buf, nbytes);
  16. close(fd);
  17. fwrite(buf, sizeof(char), bytes_read, stdout);
  18. printf("bytes read:%zd ", bytes_read);
  19. return 0;
  20. }