Saturday, August 30, 2014

Unix Prog: Character at-a-time I/O(2)

1. ungetc
ungetc system call is used to push back characters to file stream. Normally modern system only allows pushing back one character.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h  
 ......  
 /* Push a character back onto the input buffer of STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int ungetc (int __c, FILE *__stream);  
 ......  

fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp;  
   
  // Open the file "test.txt" containing only one character 'a'  
  if((fp = fopen("test.txt", "r+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  // Read 2 characters, 2nd one is EOF  
  printf("%c ", getc(fp));  
  printf("%d\n", fgetc(fp));  
   
  // Push the character 'k' back to file stream  
  if(ungetc('k', fp) != 'k') {  
   printf("ungetc error!\n");  
   exit(1);  
  }  
   
  // Read 2 characters again  
  printf("%c ", getc(fp));  
  printf("%d\n", fgetc(fp));  
   
  fclose(fp);  
   
  exit(0);  
 }  

shell:
Run the program opening the "test.txt" containing only one character. It firstly read 2 characters to encounter the EOF. And use "ungetc" system call to push a character 'k' back to stream, this action will clear up file stream's EOF flag, at this time, file stream contains only 1 character 'k'(not on disk). Lastly, it read 2 characters again to encounter the EOF.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 a -1  
 k -1  

2. Character output I/O
putc, fputc, and putchar are used to output the character I/O stream.

Definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h  
 ......  
 /* Write a character to STREAM.  
   
   These functions are possible cancellation points and therefore not  
   marked with __THROW.  
   
   These functions is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int fputc (int __c, FILE *__stream);  
 extern int putc (int __c, FILE *__stream);  
   
 /* Write a character to stdout.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern int putchar (int __c);  
 ......  

fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char *argv[])  
 {  
  FILE *fp;  
   
  if((fp = fopen("test.txt", "w+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  // Use putc to write 'a' to file stream  
  if(putc('a', fp) != 'a') {  
   printf("putc error!\n");  
   exit(2);  
  }  
   
  // Use fputc to write 'b' to file stream  
  if(fputc('b', fp) != 'b') {  
   printf("fputc error!\n");  
   exit(3);  
  }  
   
  fclose(fp);  
   
  // Use putchar to write 'k' to stdout stream  
  putchar('k');  
  exit(0);  
 }  

shell:
Run the program who write 'a' 'b' to test.txt and then write 'k' to standard output.
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 kubuntu@ip-172-31-23-227:~$ cat test.txt  
 ab  

No comments:

Post a Comment