#include<stdio.h> main() { char a[ ]="CCIT\nRajapeth\nAmravati"; FILE *fp; fp=fopen("c:\\temp\\info.txt","w"); if(fp==NULL) printf("Cannot Open File"); else { fputs(a,fp); fclose(fp); printf("Data stored.."); } }
Data stored.. ------------------------------ CCIT Rajapeth Amravati
#include<stdio.h> main() { char arr[100]; int i=1; FILE *fp; fp=fopen("c:\\temp\\info.txt","r"); if(fp==NULL) printf("Cannot Open File"); else { while(fgets(arr,100,fp)!=NULL) { printf("%d %s",i,arr) ; i++; } fclose(fp); } }
1 CCIT 2 Rajapeth 3 AmravatiWelcome to CCIT
#include<stdio.h> main() { FILE *fp; char name[100]; int rollno,age; printf("Enter the Rollno,age,name => \n"); scanf("%d %d %s",&rollno,&age,name); fp=fopen("stud.txt","a"); if(fp==NULL) printf("File dos'nt exists..."); else { fprintf(fp,"%d %d %s",rollno,age,name); fclosel(fp); } }
Enter the Rollno,age,name => 3112 21 Amit
#include<stdio.h> main() { FILE *fp; n[100]; int rollno,age; =fopen("stud.txt","r"); if(fp==NULL) printf("File dos'nt exists..."); else { while(fscanf(fp,"%d %d %s",&rollno,&age,n)==3) printf("RollNo %d Age %d Name %s\n",rollno,age,n); fclose(fp); } }
RollNo 3112 Age 21 Name Amit RollNo 2113 Age 52 Name Gopal