Tuesday 24 February 2015

FILE Handling through C++

Functions

Fopen()
Fgets()
Fputs()

Fclose()

#include<iostream.h>
#include<conio.h>

void main()
{
FILE *fp;
Char name[40];
Char ans=’y’;
Int age;
Float bs;
Clrscr();
Fp=fopen(“EMPLOYEE.DAT”,”w”);
If(fp==NULL)
{
cout<<“Cannot open file”;
exit();
}
while(ans= =’Y’ || ans= =’y’)
{
cout<<“\n Enter name, age and basic salary:\n”;
cin>>name>>age>>bs;
fputs(fp,”%s%d%f\n”,name,age,bs);
cout<<”Another employee(y/n)”;
cin>>ans;
}
fclose(fp);
}

#include<iostream.h>
#include<conio.h>

void main()
{
FILE *fp;
Char name[40];
Int age;
Float bs;
Clrscr();
Fp=fopen(“EMPLOYEE.DAT”,”r”);
If(fp==NULL)
{
cout<<“Cannot open file”;
exit(0);
}
while(fgets(fp,”%s%d%f”,&name,&age,&bs)!=EOF)
cout<<name<<age<<bs;
fclose(fp);
}

#include<iostream.h>
#include<conio.h>
void main()
{
FILE *infile;
Char name[20];
Char street[20];
Char city[15];
Char pin[7];
Int cho=1;
Char s[20];
Clrscr();
Cout<<”File name?\n”;
Cin>>s;
Infile=fopen(s,”a”);
While(cho==1)
{
cout<<“\n Type your name:”;
cin>>name;
cout<<“\n Type your street name:”;
cin>>street;
cout<<“\n Type your city name:”;
cin>>city;
cout<<“\n Type your pincode:”;
cin>>pin;
fputs(infile,”%s\t”,name);
fputs(infile,”%s\t”,street);
fputs(infile,”%s\t”,city);
fputs(infile,”%s\t”,pin);
fputs(infile,”\n”);
cout<<“Enter 1 to continue:”;
cin>>cho;
}
fclose(infile);
}

#include<iostream.h>
#include<conio.h>

void main()
{
FILE *infile;
Char name[20];
Char street[20];
Char city[15];
Char pin[7];
Char s[20];
Clrscr();
Cout<<“File Name?\n”;
Cin>>s;
If((infile=fopen(s,”r”))=NULL)
{
cout<<“Cannot open file”;
exit();
}
while(fgets(infile,”%s%s%s%s”,name,street,city,pin)!=EOF)
{
cout<<“\n Name:\t”<<name;
cout<<“\n Street:\t”<<street;
cout<<“\n City:\t”<<city;
cout<<“\n Pin:”\t”<<pin;
printf(“\n”);
}
fclose(infile);
getch();
}

No comments:

Post a Comment