C++ Assignment 11
C++ Assignment 11
C++ Assignment 11
Q2- Explain various types of mode in C++ used for file handling
with proper code?
iv) ios::truncate: - Opens a file for writing. If file already exists then
its previous contents are replaced with the new contents.
v) ios::app: - Opens a file in append mode. It adds new content at the
end of the file. Program related to this mode is as follow: -
#include<fstream>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class student
{
int rollno;
char name[20];
char branch[3];
float marks;
char grade;
public:
void getdata()
{
cout<<"Rollno: ";
cin>>rollno;
cout<<"Name: ";
cin>>name;
cout<<"Branch: ";
cin>>branch;
cout<<"Marks: ";
cin>>marks;
if(marks>=75)
{
grade = 'A';
}
else if(marks>=60)
{
grade = 'B';
}
else if(marks>=50)
{
grade = 'C';
}
else if(marks>=40)
{
grade = 'D';
}
else
{
grade = 'F';
}
}
void putdata()
{
cout<<name<<", rollno "<<rollno<<" has ";
cout<<marks<<"% marks and "<<grade<<"
grade."<<"\n";
}
int getrno()
{
return rollno;
}
}stud1;
int main()
{
student obj;
system("cls");
ofstream fout("marks.dat", ios::app);
char ans='y';
while(ans=='y' || ans=='Y')
{
stud1.getdata();
fout.write((char *)&stud1, sizeof(stud1));
cout<<"Data appended in the file successfully..!!\n";
cout<<"\nWant to enter more ? (y/n)..";
cin>>ans;
}
obj.putdata();
fout.close();
cout<<"\nPress any key to exit.\n";
getch();
}
vi) ios::ate” - It is used to write new contents at the end of the file and
we can also modify the previous contents.
vii) ios::nocreate: - It does not allow to create a new file if it does not
exist.
viii) ios::noreplace: - It does replace the old file with new file.