WAP to create a single file and then display its contents.

//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


Comments

Popular posts from this blog

WAP using a class to store price list of 5 items and to print the largest prices as well as the sum of all prices.

WAP that obtains the rotation factor and rotates the array as per it.

WAP to get roll numbers and marks of the student of a class (get from the user) and store these details into a file called Marks.dat