//WAP to create a single file and then display its contents.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
system("cls");
ofstream fout("student", ios::out);
char name[30], ch;
float marks =0.0;
//Loop to get 5 records
for(int i=0;i<5;i++)
{
cout<<"Student "<<(i+1)<<":\tName: ";
cin.get(name,30);
cout<<"\t\tMarks: ";
cin>>marks;
cin.get(ch); //to empty input buffer write to the file
fout<<name<<"\n"<<marks<<"\n";
}
fout.close(); //disconnect student file from fout
ifstream fin("student", ios::in); //connect student file to input stream fin
fin.seekg(0); //To bring file pointer at the file beginning
cout<<"\n";
for(i=0;i<5;i++) //Display records
{
fin.get(name,30); //read name from file student
fin.get(ch);
fin>>marks; //read marks from file student
fin.get(ch);
cout<<"Student Name: "<<name;
cout<<"\tMarks: "<<marks<<"\n";
}
fin.close(); //disconnect student file from fin stream
getch();
}
OUTPUT
No comments:
Post a Comment