#include<stdio.h> main() { FILE *fp; int i,arr[10]={343,2355,3232,55,222,55,222,666,3232, 5545}; fp=fopen("nos.dat","wb"); if(fp==NULL) printf("can not open File "); else { printf("Data Stored"); fwrite(arr,40,1,fp); fclose(fp); } }
Data stored..
#include<stdio.h> main() { FILE *fp; int i,a[10],s=0; fp=fopen("nos.txt","rb"); if(fp==NULL) printf("Fil does'nt exists......"); else { fread(a,40,1,fp); /* read 40 bytes from file */ for(i=0;i<=9;i++) s=s+a[i]; printf("Sum of all nos is %d",s); fclose(fp); } }
Sum of all nos is 15927
#include<stdio.h> main() { FILE *fp; int a,b,c; fp=fopen("nos.txt","rb"); if(fp==NULL) printf("File does'nt exists......"); else { fseek(fp , 4 , SEEK_SET); fread(&a , 4 , 1 , fp); fseek(fp , 8 , SEEK_CUR); fread(&b , 4 , 1 , fp); fseek(fp , - 8 , SEEK_END); fread(&c , 4 , 1 , fp); printf("The numbers are %d %d %d ",a,b,c); fclose(fp); } }
The numbers are 2355 222 3232