Program to store and read roll,name and percentage of some students to /from a data file using same program.

using codeblocks
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
int main()
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
printf("we are going to write /store data\n");
printf("\nenter roll\n");   //data input
printf("enter percentage\n");
printf("enter  name\n");           // getting inputs
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("read records are:\n");
k=fopen("student.txt","r");//file opening
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file
return 0;

No comments:

Post a Comment