C Program to Write on Data File and Read From Data File

C Program to Write on Data File and Read From Data File.

For Reading and Writing to a text file, we use the functions fprintf( ) and fscanf( ). 

They are just the file versions of printf( ) and scanf( ). The only difference is that, fprint and fscanf expects a pointer to the structure FILE.


The Following example illustrates the Write to a text file using fprintf( )

--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
int main( )
{
   int num;
   FILE *fptr;
   fptr = fopen("c:\\program.txt", "w");
   if(fptr == NULL)
  {
       printf("Error! ");
       exit(1);
  }
  printf("Enter num: ");
  scanf("%d", &num);
  fprintf(fptr,"%d",num);
  fclose(fptr);
  return 0;
}

--------------------------------------------------------------------------------------------------------------------------

The Following example illustrates the Read from a text file using fscanf( )

#include<stdio.h>
int main( )
{
    int num;
   FILE *fptr;
  if((fptr = fopen("c:\\program.txt", "r")) == Null)
 {
     printf("Error! opening file");
     exit(1);
 }
fscanf(fptr,"%d",&num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

Functions fread( ) and fwrite( ) are used for reading from and writing to a file on the disk respectively in case of binary files.
To write into a binary file, you need to use the function fwrite( ). The functions takes 4 arguments.
⦁ Address of Data to be written in Disk
⦁ Size of Data to be Written in Disk
⦁ Number of such type of Data
⦁ Pointer to the file where you want to write
     fwrite(address_data,size_data,numbers_data,pointer_to_file);


The folllowing example illustrates the writing to a binary file using fwrite( )
#include<stdio.h>
struct threeNum
{
   int n1,n2,n3;

};
int main( )
{
    int n;
    struct threeNum num;
    FILE *fptr;
    if((fptr = fopen("c:\\program.bin","wb")) == NULL)
   {
       printf("Error! opening file");
       exit(1);
   }
for(n=1;n<5;++n)
{
    num.n1 = n;
    num.n2 = 5n;
    num.n3 = 5n+1;
    fwrite(&num, sizeof(struct threeNum), 1,fptr);
}
fclose(fptr);

return 0;
}


The function fread( ) also takes 4 arguments similar to fwrite( ) function.
      fread(address_data,size_data,numbers_data,pointer_to_file);

The folllowing example illustrates the writing to a binary file using fwrite( )

#include<stdio.h>
struct threeNum
{
   int n1,n2,n3;
};
int main( )
{
    int n;
    struct threeNum num;
    FILE *fptr;
    if((fptr = fopen("c:\\program.bin","rb")) == NULL)
   {
       printf("Error! opening file");
       exit(1);
   }
for(n=1;n<5;++n)
{
    fread(&num, sizeof(struct threeNum), 1,fptr);
    printf("n1: %d\tn2: %d\tn3: %d", num.n1, num.n2,num.n3);
}
fclose(fptr);
return 0;
}

--------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
struct student
{
   int roll;
   char name[12];
   int percent;
} s1 = {10,"SMJC"80);
int main( )
{
   FILE *fp;
   struct student s2;
   fp = fopen("ip.txt", "w");
   fwrite(&s1,sizeof(s1),1,fp);
   fclose(fp);
   fp = fopen("ip.txt","r");
   fread(&s2,sizeof(s2),1,fp);
   fclose(fp);
   printf("\n Roll : %d",s2.roll);
   printf("\n Name : %s",s2.name);
   printf("\n Percent : %d",s2.percent);
   return(0);
}

--------------------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------------------

1 Roll : 10
2 Name : SMJC
3 Percent : 80



EmoticonEmoticon