Showing posts with label Write on Data File and Read From Data File.. Show all posts
Showing posts with label Write on Data File and Read From Data File.. Show all posts

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

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



C Program to Write on Data File and Read from Data File Images-Pictures-Photos-Pics-Snaps-Basic C Programs-Files in C-File Handling in C-File Operations in C-File Programs-C Language Programs-File Modes in C-C Program to Write on Data File-C Program to Read from Data File-General C Programs.





C Program to Write on Data File and Read from Data File Images-Pictures-Photos-Pics-Snaps-Basic C Programs-Files in C-File Handling in C-File Operations in C-File Programs-C Language Programs-File Modes in C-C Program to Write on Data File-C Program to Read from Data File-General C Programs.


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


Kategori

Kategori