Sunday, August 17, 2014

Unix Prog: File I/O(4)

1. read and write the file with lseek
fileio.c
 #include<unistd.h>  
 #include<fcntl.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<errno.h>  
   
 int main()  
 {  
  char buf[]="Hello world!";  
  int fd;  
   
  // Open the test.txt with O_APPEND, meaning that current offset  
  // is at the end of the file  
  errno = 0;  
  if((fd = open("test.txt", O_RDWR | O_APPEND)) == -1) {  
   printf("Open file error! errno: %d\n", errno);  
   exit(1);  
  }  
   
  // Setup the offset by current offset, and it will return the  
  // current offset  
  errno = 0;  
  int offset;  
  if((offset = lseek(fd, 0, SEEK_CUR)) == -1) {  
   printf("can not seek! errno: %d\n", errno);  
   exit(2);  
  }  
   
  printf("Current offset when O_APPEND is set: %d\n", offset);  
  // Output: Current offset when O_APPEND is set: 0  
  // In current implementation, it will just return the offset compared  
  // to current position  
   
  // Write up sth based on current offset(the end of file)  
  errno = 0;  
  if((write(fd, buf, 12)) == -1) {  
   printf("write error! errno: %d\n", errno);  
   exit(3);  
  }  
   
  close(fd);  
  exit(0);  
 }  

shell:
it writes the content into the end of file
 ubuntu@ip-172-31-23-227:~$ cat test.txt  
 New York  
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 Current offset when O_APPEND is set: 0  
 ubuntu@ip-172-31-23-227:~$ cat test.txt  
 New York  
 Hello world!  

=====================================================================

fileio.c
 #include<unistd.h>  
 #include<fcntl.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<errno.h>  
   
 int main()  
 {  
  char buf[]="Hello world!";  
  int fd;  
   
  // Open the test.txt without O_APPEND, meaning that current position  
  // is at the beginning of the file  
  errno = 0;  
  if((fd = open("test.txt", O_RDWR)) == -1) {  
   printf("Open file error! errno: %d\n", errno);  
   exit(1);  
  }  
   
  // Setup the offset from the beginning  
  errno = 0;  
  int offset;  
  if((offset = lseek(fd, 0, SEEK_SET)) == -1) {  
   printf("can not seek! errno: %d\n", errno);  
   exit(2);  
  }  
   
  // write the buffer into the file from the beginning  
  errno = 0;  
  if((write(fd, buf, 12)) == -1) {  
   printf("write error! errno: %d\n", errno);  
   exit(3);  
  }  
   
  close(fd);  
  exit(0);  
 }  

shell:
 ubuntu@ip-172-31-23-227:~$ cat test.txt  
 Amazing!!!!!  
 Hello world!  
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 ubuntu@ip-172-31-23-227:~$ cat test.txt  
 Hello world!  
 Hello world!  
   

2. Write the file with "hole"
fileio.c
 #include<unistd.h>  
 #include<fcntl.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<errno.h>  
   
 int main()  
 {  
  char buf1[]="Hello world!";  
  char buf2[]="HELLO WORLD!";  
  int fd;  
   
  // Create the file  
  errno = 0;  
  if((fd = open("test.txt", O_RDWR | O_TRUNC, S_IRWXU)) == -1) {  
   printf("Create file error! errno: %d\n", errno);  
   exit(1);  
  }  
   
  // Write the string into the file  
  errno = 0;  
  if(write(fd, buf1, 12) != 12) {  
   printf("Write file error! errno: %d\n", errno);  
   exit(2);  
  }  
   
  // Set up the position to 16384 from the beginning  
  errno = 0;  
  if (lseek(fd, 16384, SEEK_SET) == -1) {  
   printf("Seek error! errno: %d\n", errno);  
   exit(3);  
  }  
   
  // Write the string from position 16384  
  if (write(fd, buf2, 12) != 12) {  
   printf("write file error! errno: %d\n", errno);  
   exit(4);  
  }  
  exit(0);  
 }  

shell:
1) After running io.out, test.txt is created with the size 16396(since we write 12 bytes after position 16384)
2) Using cat to print out the file's content, the hole is hidden
3) But using od -c to print out the file's content byte by byte, the hole is represented by 0, and if using read to read out info, it will be also 0.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 ubuntu@ip-172-31-23-227:~$ ls -lrt test.txt  
 -rw-rw-r-- 1 ubuntu ubuntu 16396 Aug 17 14:19 test.txt  
 ubuntu@ip-172-31-23-227:~$ cat test.txt  
 Hello world!HELLO WORLD!ubuntu@ip-172-31-23-227:~$ od -c test.txt  
 0000000  H  e  l  l  o    w  o  r  l  d  ! \0 \0 \0 \0  
 0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0  
 *  
 0040000  H  E  L  L  O    W  O  R  L  D  !  
 0040014  

No comments:

Post a Comment