C examples for File:Text File
Read Structures Stored in a Text File using the function fscanf()
#include <stdio.h> int main(){//www . j ava 2 s . com int k = 0, m = 0; FILE *fptr; struct biodata{ char name[15]; int rollno; int age; float weight; }; struct biodata sa; fptr = fopen("C:\\Code\\data.dat", "r"); if (fptr != NULL) { printf("File data.dat is opened successfully.\n"); m = fscanf(fptr, "%s %d %d %f", sa.name,&sa.rollno,&sa.age,&sa.weight); while(m != EOF){ printf("Name: %s, Roll no: %d, Age: %d, Weight: %.1f\n",sa.name, sa.rollno,sa.age, sa.weight); m = fscanf(fptr, "%s %d %d %f", sa.name,&sa.rollno,&sa.age,&sa.weight); } k = fclose(fptr); if(k == -1) puts("File-closing failed"); if(k == 0) puts("File is closed successfully."); } else puts("File-opening failed"); return(0); }