-->
Showing posts with label Program to store and read roll. Show all posts
Showing posts with label Program to store and read roll. Show all posts

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
#include<conio.h>
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");
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
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("-----------------------------------------------\n");
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
getch();
return 0;
}