-->
Showing posts with label program to understand fscanf() and fprintf().. Show all posts
Showing posts with label program to understand fscanf() and fprintf().. Show all posts

Program to store roll,name and percentage of some students in a data file

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
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                                 
}while(choice!='n');        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

//or
#include<stdio.h>
#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");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}


---------------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------------------------------------------
//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
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                                 
}while(choice!='n');        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

//or
#include<stdio.h>
#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");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}