Search This Blog

Friday 3 June 2016

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



No comments:

Post a Comment

How do I crack the GSOC in the field of machine learning?

How do I crack the GSOC in the field of machine learning? Working as a student at Google Summer of Code demands for your dedication an...