File Input/Output is used to store or fetch desirable date using C language. It is basically come in use when we want to store data and make sure it's privacy.
So, Here a Simple program for the creation of a Text format file using C language that comes under File Input/Output.
/*Pre Processors used throughout the program*/
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp; /*Creation of a file type pointer*/
char ch='Y';
struct emp /*Structure of a user defined data type i.e emp*/
{
char name[40];
int age;
float bs;
};
clrscr();
struct emp e;
fp=fopen("Employee.txt","w"); /*create a txt format file using fopen*/
if(fp==NULL)
{
puts("\nCannot open file");
}
while(ch=='Y')
{
printf("\nEnter your data");
printf("\nEnter Name:- ");
scanf("%s",&e.name);
printf("\nEnter Age:- ");
scanf("%d",&e.age);
printf("\nEnter Salary:- ");
scanf("%f",&e.bs);
fprintf(fp,"Name:- " );
fprintf(fp,"%s \n",e.name);
fprintf(fp,"Age:- ");
fprintf(fp,"%d \n",e.age);
fprintf(fp,"Salary:- ");
fprintf(fp,"%f \n\n",e.bs);
printf("\nDo you want to add another data(Y/N):- ");
fflush(stdin);
ch=getche();
}
fclose(fp); /*closing of file using fclose*/
return 0;
}
And the file that store hold the data of your program is below:-
So, Here a Simple program for the creation of a Text format file using C language that comes under File Input/Output.
/*Pre Processors used throughout the program*/
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp; /*Creation of a file type pointer*/
char ch='Y';
struct emp /*Structure of a user defined data type i.e emp*/
{
char name[40];
int age;
float bs;
};
clrscr();
struct emp e;
fp=fopen("Employee.txt","w"); /*create a txt format file using fopen*/
if(fp==NULL)
{
puts("\nCannot open file");
}
while(ch=='Y')
{
printf("\nEnter your data");
printf("\nEnter Name:- ");
scanf("%s",&e.name);
printf("\nEnter Age:- ");
scanf("%d",&e.age);
printf("\nEnter Salary:- ");
scanf("%f",&e.bs);
fprintf(fp,"Name:- " );
fprintf(fp,"%s \n",e.name);
fprintf(fp,"Age:- ");
fprintf(fp,"%d \n",e.age);
fprintf(fp,"Salary:- ");
fprintf(fp,"%f \n\n",e.bs);
printf("\nDo you want to add another data(Y/N):- ");
fflush(stdin);
ch=getche();
}
fclose(fp); /*closing of file using fclose*/
return 0;
}
And the file that store hold the data of your program is below:-
No comments:
Post a Comment