C - File I/O - Binary                                                                         Home : www.sharetechnote.com

 

 

 

 

File I/O - Binary

 

 

 

 

writing a number - fwrite()  =============================================

 

Example 01 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   int x = 0, i = 0;

 

   fp = fopen ("TestFile.bin", "wb");

   

   for(i=0; i<10 ;i++) {

       x = i;

       fwrite(&x,sizeof(int),1,fp);

   };

 

   fclose(fp);

   

   return(0);

}

 

Result :

 

 

Example 02 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   int i = 0;

   float x = 0.0;

 

   fp = fopen ("TestFile.bin", "wb");

   

   for(i=0; i<10 ;i++) {

       x = (float)i;

       fwrite(&x,sizeof(float),1,fp);

   };

 

   fclose(fp);

   

   return(0);

}

 

Result :

 

 

Example 03 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

 

   int i = 0;

   double x = 0.0;

 

   fp = fopen ("TestFile.bin", "wb");

   

   for(i=0; i<10 ;i++) {

       x = (double)i;

       fwrite(&x,sizeof(double),1,fp);

   };

 

   fclose(fp);

   

   return(0);

}

 

Result :

 

 

Example 04 >

 

#include <stdio.h>

#include <stdlib.h>

 

struct myNum

{

   int x;

   double y;

};

 

int main()

{

   FILE * fp;

   struct myNum stNum;

   int i = 0;

 

   fp = fopen ("TestFile.bin", "wb");

   

   for(i=0; i<10 ;i++) {

       stNum.x = i;

       stNum.y = (double)i;

       fwrite(&stNum,sizeof(stNum),1,fp);

   };

 

   fclose(fp);

   

   return(0);

}

 

 

Result :

 

 

writing characters - fwrite()  =============================================

 

 

Example 01 >

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   FILE * fp;

   char *s = "ABCDEFGHIJKLMNOP";

   char c;

 

   fp = fopen ("TestFile.bin", "wb");

   

   do{

        c = *(s++);

        fwrite(&c,sizeof(char),1,fp);

   }while(c != '\0');

 

   fclose(fp);

   

   return(0);

}

 

 

Result :

 

 

writing mixed data types - fwrite()  =============================================

 

Example 01 >

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct StudentRecord

{

   int ID;

   char firstName[20];

   char lastName[20];

   float Credit;

};

 

int main()

{

   FILE * fp;

   struct StudentRecord sRec;

   int i = 0;

 

   fp = fopen ("TestFile.bin", "wb");

   

   sRec.ID = 1;

   strcpy(sRec.firstName,"Jaeku");

   strcpy(sRec.lastName,"Ryu");

   sRec.Credit = 4.5;

   

   fwrite(&sRec,sizeof(sRec),1,fp);

   

   sRec.ID = 2;

   strcpy(sRec.firstName,"James");

   strcpy(sRec.lastName,"Williams");

   sRec.Credit = 3.9;

   

   fwrite(&sRec,sizeof(sRec),1,fp);

 

   fclose(fp);

   

   return(0);

}

 

Result :

 

 

reading numbers - fread()  =============================================

 

Example 01 >

 

File to read from :

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

   FILE * fp;

   char c;

   int i = 0;

   int x = 0;

 

   fp = fopen ("TestFile.bin", "rb");

   

   while(!feof(fp))

   {

        fread(&x,sizeof(int),1,fp);

        

        printf("%d",x);

   } ;

      

   fclose(fp);

   

   return(0);

}

 

 

Result :

01234567899

 

 

Example 02 >

 

 

 

#include <stdlib.h>

#include <string.h>

 

int main()

{

   FILE * fp;

   char c;

   int i = 0;

   int x = 0;

 

   fp = fopen ("TestFile.bin", "rb");

   

   for(;;)

   {

        fread(&x,sizeof(int),1,fp);

        

        if(feof(fp))

          break;

        else

          printf("%d",x);   

   } ;

      

   fclose(fp);

   

   return(0);

}

 

 

Result :

0123456789

 

 

 

reading characters (string) - fread()  =============================================

 

Example 01 >

 

File to Read from :

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

int main()

{

   FILE * fp;

   char c;

   int i = 0;

 

   fp = fopen ("TestFile.bin", "rb");

   

   while(!feof(fp))

   {

        fread(&c,sizeof(char),1,fp);

        

        printf("%c",c);

   } ;

      

   fclose(fp);

   

   return(0);

}

 

Result :

ABCDEFGHIJKLMNOP

 

 

 

reading mixed data type - fread()  =============================================

 

Example 01 >

 

File to Read from :

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct StudentRecord

{

   int ID;

   char firstName[20];

   char lastName[20];

   float Credit;

};

 

int main()

{

   FILE * fp;

   struct StudentRecord sRec;

   int i = 0;

 

   fp = fopen ("TestFile.bin", "rb");

   

   while(!feof(fp))

   {

        fread(&sRec,sizeof(sRec),1,fp);

        

        printf("Studend ID = %d\n",sRec.ID);

        printf("First Name = %s\n",sRec.firstName);

        printf("Last Name = %s\n",sRec.lastName);

        printf("Credit = %f\n",sRec.Credit);

        printf("\n");

   } ;

      

   fclose(fp);

   

   return(0);

}

 

Result :

Studend ID = 1

First Name = Jaeku

Last Name = Ryu

Credit = 4.500000

 

Studend ID = 2

First Name = James

Last Name = Williams

Credit = 3.900000

 

Studend ID = 2

First Name = James

Last Name = Williams

Credit = 3.900000

 

 

 

Example 02 >

 

File to Read from :

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct StudentRecord

{

   int ID;

   char firstName[20];

   char lastName[20];

   float Credit;

};

 

int main()

{

   FILE * fp;

   struct StudentRecord sRec;

   int i = 0;

 

   fp = fopen ("TestFile.bin", "rb");

   

   while(!feof(fp))

   {

        fread(&sRec,sizeof(sRec),1,fp);

        

        printf("Studend ID = %d\n",sRec.ID);

        printf("First Name = %s\n",sRec.firstName);

        printf("Last Name = %s\n",sRec.lastName);

        printf("Credit = %f\n",sRec.Credit);

        printf("\n");

   } ;

      

   fclose(fp);

   

   return(0);

}

 

Result :

Studend ID = 1

First Name = Jaeku

Last Name = Ryu

Credit = 4.500000

 

Studend ID = 2

First Name = James

Last Name = Williams

Credit = 3.900000