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.
--------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
struct student
{
int roll;
char name[12];
int percent;
} s1 = {20,"KIPL"60);
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 : 20
2 Name : KIPL
3 Percent : 60
EmoticonEmoticon