Sunday, August 31, 2014

Unix Prog: Position Stream(1)

1. System Call Definition
ftell, fseek, rewind are used to give current cursor position of the stream, and re-position the cursor in stream.
ftello, fseeko are almost same as ftell and fseek, the only difference is it use type "off_t" to define the offset, and ftell fseek use "long". "off_t" is implementation dependent, it could be much longer at some systems.

definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h  
 ......  
 /* Seek to a certain position on STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int fseek (FILE *__stream, long int __off, int __whence);  
 /* Return the current position of STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern long int ftell (FILE *__stream) __wur;  
 /* Rewind to the beginning of STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern void rewind (FILE *__stream);  
 __END_NAMESPACE_STD  
 ......  
 /* Seek to a certain position on STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int fseeko (FILE *__stream, __off_t __off, int __whence);  
 /* Return the current position of STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern __off_t ftello (FILE *__stream) __wur;  
 ......  

2. Tell and seek text file position
fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp = fopen("test.txt", "w+");  
  long pos;  
   
  // Write "Hello world!" into text file test.txt  
  if(fputs("Hello world!", fp) < 0) {  
   printf("fputs error!\n");  
   exit(1);  
  }  
   
  // Get the current position: 12  
  if((pos = ftell(fp)) < 0) {  
   printf("ftell error!\n");  
   exit(2);  
  }  
   
  printf("current text file position: %ld\n", pos);  
   
  // Set up the current position to the beginning of test.txt file  
  if(fseek(fp, 0, SEEK_SET) != 0) {  
   printf("fseek error!\n");  
   exit(3);  
  }  
   
  if((pos = ftell(fp)) < 0) {  
   printf("ftell error!\n");  
   exit(2);  
  }  
   
  printf("current text file position: %ld\n", pos);  
   
  // Write "Amazing world!" to file test.txt from beginning  
  // Original words "Hello world!" are replaced.  
  if(fputs("Amazing world!\n", fp) < 0) {  
   printf("fputs error!\n");  
   exit(1);  
  }  
   
  // rewind command will move cursor back to the beginning of text file  
  // And read the text out  
  rewind(fp);  
  char buff[BUFSIZ];  
  if(fgets(buff, BUFSIZ, fp) != buff) {  
   printf("fgets error!\n");  
   exit(3);  
  }  
   
  printf("content read out: %s\n", buff);  
   
  fclose(fp);  
  exit(0);  
 }  

shell:
Run the program, it output the position after writing out "Hello world!", and another position after moving cursor to the beginning of file.
Note that we have one more line after the "Amazing world!", since fgets already read one newline operator, and printf add another newline at the end, so we have one more empty line here.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 current text file position: 12  
 current text file position: 0  
 content read out: Amazing world!  
   

No comments:

Post a Comment