Sunday, August 31, 2014

Unix Prog: Binary I/O(1)

1. fread, fwrite Definition
We could use fread and fwrite to binary structure into file, which may be painful if using the character, line I/O

definition:
 ubuntu@ip-172-31-23-227:~$ less /usr/include/stdio.h  
 ......  
 /* Read chunks of generic data from STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern size_t fread (void *__restrict __ptr, size_t __size,  
            size_t __n, FILE *__restrict __stream) __wur;  
 /* Write chunks of generic data to STREAM.  
   
   This function is a possible cancellation point and therefore not  
   marked with __THROW. */  
 extern size_t fwrite (const void *__restrict __ptr, size_t __size,  
            size_t __n, FILE *__restrict __s);  
 ......  

2. read and write basic type

fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp;  
   
  // Write the file numerical numbers into binary file test  
  if((fp = fopen("test", "w+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  float tf[5] = {1.22, 2.33, 3.44, 4.55, 5.66};  
  if(fwrite(tf, sizeof(float), 5, fp) != 5) {  
   printf("fwrite error!\n");  
   exit(2);  
  }  
   
  fclose(fp);  
   
  // Re-open the file to make cursor back to beginning  
  // And the read back all numerical numbers  
  if((fp = fopen("test", "r+")) == NULL) {  
   printf("fopen error!\n");  
   exit(3);  
  }  
   
  float rtf[5];  
  if(fread(rtf, sizeof(float), 5, fp) != 5) {  
   printf("fread error!\n");  
   exit(3);  
  }  
   
  int i;  
  for(i = 0; i < 5; i++) {  
   printf("%f ", rtf[i]);  
  }  
  printf("\n");  
   
  fclose(fp);  
   
  exit(0);  
 }  

shell:
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 1.220000 2.330000 3.440000 4.550000 5.660000  

3. read and write struct
fileio.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<string.h>  
   
 struct ts {  
  float tf;  
  int ti;  
  char tc[13];  
 };  
   
 int main(int argc, char* argv[])  
 {  
  FILE *fp;  
  struct ts temp;  
   
  // Write the struct temp into binary file test  
  if((fp = fopen("test", "w+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  temp.tf = 1.22;  
  temp.ti = 2;  
  strcpy(temp.tc, "Hello world!");  
   
  if(fwrite(&temp, sizeof(temp), 1, fp) != 1) {  
   printf("fwrite error!\n");  
   exit(2);  
  }  
   
  fclose(fp);  
   
  // Re-open the file to move cursor back to beginning  
  // To read the struct back  
  if((fp = fopen("test", "r+")) == NULL) {  
   printf("fopen error!\n");  
   exit(1);  
  }  
   
  struct ts temp_read;  
  if(fread(&temp_read, sizeof(temp_read), 1, fp) != 1) {  
   printf("fopen error!\n");  
   exit(3);  
  }  
   
  printf("%f, %d, %s\n", temp_read.tf, temp_read.ti, temp_read.tc);  
   
  exit(0);  
 }  

shell:
 ubuntu@ip-172-31-23-227:~$ ./io.out  
 1.220000, 2, Hello world!  

No comments:

Post a Comment