holes.c (1122B)
1 /* 2 * File holes. 3 * 4 * APUE experimental - Just to understand. 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <fcntl.h> 11 #include <sys/stat.h> 12 13 #define FILE_NAME "test.hole" 14 #define BUFSZ 512 15 16 int 17 main(void) 18 { 19 int fd; 20 char before[BUFSZ]; 21 char after[] = "This is the text written after the hole"; 22 23 for(fd = 0; fd < BUFSZ; fd++) 24 before[fd] = 'X'; 25 26 /* open the file */ 27 if( (fd = open(FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU)) == -1 ) { 28 perror("open"); 29 return EXIT_FAILURE; 30 } 31 32 /* write few data */ 33 if( write(fd, before, sizeof(before)) != sizeof(before) ) { 34 perror("write"); 35 close(fd); 36 return EXIT_FAILURE; 37 } 38 39 /* seek after the end of file */ 40 if( lseek(fd, BUFSZ * BUFSZ, SEEK_SET) < 0 ) { 41 perror("lseek"); 42 close(fd); 43 return EXIT_FAILURE; 44 } 45 46 /* write few data (make the hole) */ 47 if( write(fd, after, sizeof(after)) != sizeof(after) ) { 48 perror("write"); 49 close(fd); 50 return EXIT_FAILURE; 51 } 52 53 close(fd); /* close the file */ 54 55 return EXIT_SUCCESS; 56 } /* eof main() */ 57