Write-Read (using fprint & fscan) in C.

 

01  #include<stdio.h>
02
03 int main()
04 {
05 char name[20];
06 int id;
07 float cgpa;
08
09 FILE *fp; // file pointer
10
11 printf("Enter your Details :");
12 gets(name);
13 scanf("%d %f",&id,&cgpa);
14
15 fp=fopen("c:\student.txt","w"); //open a file for writing (file_path,mode)
16
17 fprintf(fp,"%s %d %f",name,id,cgpa);
18
19 fclose(fp); //closing a file
20
21
22 fp=fopen("c:\student.txt","r"); //open a file for reading (file_path,mode)
23
24 fscanf(fp,"%s %d %f",name,&id,&cgpa);
25
26 fclose(fp); // closing the file
27
28 printf("Name : %snID : %dnCGPA : %f",name,id,cgpa);
29
30 return 0;
31 }
 
 
OUTPUT :

Enter your Details :
Amit
1231
8.7

Name : Amit
ID : 1231
CGPA : 8.700000

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *