Sunday, August 31, 2014

Unix Prog: Position Stream(2)

1. Tell and seek binary file

fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<string.h>  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp;  
   
  // Open the test file  
  if((fp = fopen("test", "w+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  // Write a string into binary file  
  int ti[3]={1,2,3};  
   
  if(fwrite(ti, sizeof(int), 3, fp) != 3) {  
   printf("fwrite error!\n");  
   exit(2);  
  }  
   
  // Read current position  
  long pos;  
   
  if((pos = ftell(fp)) < 0) {  
   printf("ftell error!\n");  
   exit(3);  
  }  
   
  printf("current binary file position: %ld\n", pos);  
   
  // Rewind to the beginning of file  
  if(fseek(fp, 0, SEEK_SET) != 0) {  
   printf("fseek error!\n");  
   exit(4);  
  }  
  printf("current binary file position: %ld\n", ftell(fp));  
   
  // Write small string to file, replaces the first integer(4 bytes)  
  char tc[4]={'a','b','c','d'};  
  if(fwrite(tc, sizeof(char), 4, fp) != 4) {  
   printf("fwrite error!\n");  
   exit(2);  
  }  
   
  // Read the string and remaining 2 numbers out  
  rewind(fp);  
  char rtc[4];  
  int rti[2];  
  if(fread(rtc, sizeof(char), 4, fp) != 4) {  
   printf("fread error!\n");  
   exit(3);  
  }  
   
  if(fread(rti, sizeof(int), 2, fp) != 2) {  
   printf("fread error!\n");  
   exit(3);  
  }  
   
  printf("%c%c%c%c %d %d\n", rtc[0], rtc[1], rtc[2], rtc[3], rti[0], rti[1]);  
   
  fclose(fp);  
  exit(0);  
 }  

shell:
Run the program, it will output the position after writing 3 integers(12 bytes), and then the position after rewind the cursor to the beginning of file. At this time, it wrote 4 characters into the file, which is 4 bytes, also replaced the first integer.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 current binary file position: 12  
 current binary file position: 0  
 abcd 2 3  

fseek position mode:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h  
 ......  
 #define SEEK_SET    0    /* Seek from beginning of file. */  
 #define SEEK_CUR    1    /* Seek from current position. */  
 #define SEEK_END    2    /* Seek from end of file. */  
 ......  

2. fgetpos, fsetpos
These two functions are introduced by ISO C standard, basically they are same as above functions.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/_G_config.h  
 ......   
 typedef struct   
 {   
  __off_t __pos;   
  __mbstate_t __state;   
 } _G_fpos_t;   
 ......  
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h
 ......
 typedef _G_fpos_t fpos_t;  
 ......  
 /* Get STREAM's position.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);  
 /* Set STREAM's position.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int fsetpos (FILE *__stream, const fpos_t *__pos);  
 ......  

fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp;  
   
  // Open the file  
  if((fp = fopen("test.txt", "w+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  // Setup the position to 10  
  fpos_t pos;  
  pos.__pos = 10;  
   
  if(fsetpos(fp, &pos) != 0) {  
   printf("fsetpos error!\n");  
   exit(3);  
  }  
   
  // Get the current position  
  if(fgetpos(fp, &pos) != 0) {  
   printf("fgetpos error!\n");  
   exit(2);  
  }  
   
  printf("current position: %ld\n", pos.__pos);  
   
  fclose(fp);  
  exit(0);  
 }  

shell:
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 current position: 10  

No comments:

Post a Comment