Tobiahao

S01_PLIKI_02

Jan 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. /*
  2. Stwórz plik tekstowy wielkości 512 bajtów, z „dziurą” w środku.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11.  
  12. #define PATHNAME "02_file"
  13. #define FILESIZE 512
  14.  
  15. int main(void)
  16. {
  17.     int fd;
  18.     struct stat stat_buffer;
  19.  
  20.     if((fd = open(PATHNAME, O_CREAT | O_TRUNC | O_WRONLY, 0600)) == -1) {
  21.         perror("open");
  22.         return EXIT_FAILURE;
  23.     }
  24.  
  25.     if(lseek(fd, FILESIZE-1, SEEK_SET) == -1) {
  26.         perror("lseek");
  27.         return EXIT_FAILURE;
  28.     }
  29.  
  30.     if(write(fd, "", 1) == -1) {
  31.         perror("write");
  32.         return EXIT_FAILURE;
  33.     }
  34.  
  35.     if(stat(PATHNAME, &stat_buffer) == -1) {
  36.         perror("stat");
  37.         return EXIT_FAILURE;
  38.     }
  39.  
  40.     printf("Utworzono plik o: %ld B, zajmuje %ld sektorow\n", (long)stat_buffer.st_size, (long)stat_buffer.st_blocks);
  41.  
  42.     if(close(fd) == -1) {
  43.         perror("close");
  44.         return EXIT_FAILURE;
  45.     }
  46.  
  47.     return EXIT_SUCCESS;
  48. }
Add Comment
Please, Sign In to add comment