Professional Documents
Culture Documents
EX:
class distance
{
int feet;
float inch;
public:
feet= (int) p;
inch=(p-feet)*12;
}
void show()
{
cout<<"feet="<<feet;
cout<<"inch="<<inch;
}
};
#include <iostream.h>
#include <string.h>
#include <conio.h>
classstring
{
char *p;
int len;
public:
string()
{}
string(char *a)
{
len=strlen(a);
p=newchar[len+1];
strcpy(p,a);
}
operatorchar*()
{
return(p);
}
void display()
{
cout<<p;
}
};
void main()
{
clrscr();
string o1="vivek";
cout<<"String of Class type : ";
o1.display();
cout<<endl;
char *str=o1;
getch();
}
Q2. Explain the various methods to open a file. Also
write a program to create a file and then display
its contents on the screen with the help of open()
function.
Ans:---There are some classes provided to us for
performing many other operations other than
opening:-----------------------------------------------------------
fstreambase
Provides operations common to the file streams. Serves as a base
for fstream, ifstream and ofstream classes. Contains open() and
close() functions.
ifstream
Provides input operations. Contains open() with default input
mode. Inherits the functions get(), getline(), read(), seekg() and
tellg() functions from istream.
ofstream
Provides output operations. Contains open() with default output
mode. Inherits put(), seekp(), tellp(), and write() functions from
ostream.
fstream
Provides support for simultaneous input and output operations.
Contains open() with default input mode. Inherits all the functions
from istream and ostream classes through iostream.
Using Constructor
Create a file stream object to manage the stream using the
appropriate class. That is, the class ofstream is used to create the
output stream and the class ifstream to create the input stream.
ofstream outfile(“sample.txt”);
The above statement creates an object outfile of class ofstream
that manages the output stream. This statement also opens the
file sample.txt and attaches it to the output stream for writing.
ifstream infile(“sample.txt”);
# include
void main()
{
ofstream outfile(“sample.txt”); // create file for output
char ch = ‘a’;
int i = 12;
float f = 4356.15;
char arr[ ] = “hello”;
outfile << ch << endl <<< endl << f << endl << arr; //send the
data to file
outfile.close();
ifstream infile(“sample.txt”);
infile >> ch >> i >> f >> arr; // read data from file
#include
#include
void main()
{
char str[]=“C++ is superset of C. It is an object-oriented /
programming language.”;
The binary input and output functions read() and write() are
designed to do exactly this job.
The write() function is used to write the object of a class into the
specified file and read() function is used to read the object of the
class from the file.
#include
class Person
{
private:
char name[40];
int age;
public:
void getData()
{
cout << “\n Enter name:”; cin >> name;
cout << “\n Enter age:”; cin >> age;
}
} ; // End of the class definition
void main()
{
Person per ; // Define an object of Person class
Parameter Meaning
ios::app Append to end of the file
ios::ate Go to end of the file on opening
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
ios::out Open file for writing only
ios::trunc Delete contents of the file if it exists
int main()
{
fstream File;
string array[10];
File.open("Data.txt", ios::out);
if (File.is_open ()) {
for (int x = 0; x <= 9; x++)
{
cout<< x <<" Enter a string:"<<endl;
cin>> array[x];
File<< array[x] << ", ";
cout<<endl;
}
}
File.close();
return 0;
}
becomes as new
If FileName already exists and you try to open it, this operation
would fail
because it cannot create a file of the same name in the same
location.
Q4. Jot down the various functions for manipulation of file
pointers.
seekg
Sets the position of the get pointer. The get pointer determines
the next location to be
read in the source associated to the stream.
Syntax:
seekg ( position );
tellp
Returns the absolute position of the put pointer. The put pointer
determines the
location in the output sequence where the next output operation
is going to take place.
Syntax:
pos_type tellp();
The tellp() function is used with output streams, and returns the
current "put" position
of the pointer in the stream. It has no parameters and return a
value of the member
type pos_type, which is an integer data type representing the
current position of the put
stream pointer.
The seek functions seekg() and seekp() can also be used with two
arguments
as follows:
seekg(offset, refposition);
seekp(offset, refposition);
The parameter offset represents the number of bytes the file
pointer to be
moved from the location specified by the parameter refposition.
The refposition takes one of the following these constant defined
in the ios
class.
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file.
EX:
class person
{
private:
char name[40];
int age;
public:
void showData()
{
cout << “\n Name = “ << name;
cout << “\n Age = “ << age; } };
void main()
{
person pers; // create person object
ifstream infile; // create input file
infile.open(“Person.txt”); // open the file
infile.seekg(0, ios::end); // go to end from 0 byte
int endposition = infile.tellg(); // find where we are
int n = endposition/sizeof(person); // number of persons
cout << “\n There are “ << n << “ persons in file: “;
cout << “\n Enter person number: “;
cin >> n;
int position = (n-1) * sizeof(person); // number times size
infile.seekg(position);
infile.read( (char*)&pers, sizeof(pers) );
pers.showData(); // display the person
}
Q5. Write down a program in C++ implementing file handling that
uses various error handling functions.
Ans:-
ios::good function)
Check if the state of the stream is good for i/o operations. (public
member
#include <fstream.h>
#include <string.h>
#include <iostream.h>
using namespace std;
void check_stream_state1(const fstream &s)
{
int state = s.rdstate();
if (!state)
{
cout << "goodbit - no error occurred" << endl;
}
else
{
if (state & ios::badbit)
cout << "badbit - a fatal I/O error has occurred" << endl;
if (state & ios::eofbit)
cout << "eofbit - end of file is encountered" << endl;
if (state & ios::failbit)
cout << "failbit - a nonfatal I/O error has occurred" << endl;
}}
void check_stream_state2(const fstream &s)
{
//true if no error indicators are set
if(s.good())
{
cout << "good() returned true" << endl;
}
else
{
if (s.bad())
cout << "bad() returned true" << endl;
if (s.eof())
cout << "eof() returned true" << endl;
if(s.fail())
cout << "fail() returned true" << endl;
}}
int main()
{
fstream output("stream.txt", ios::out);
//using member function good()
if(output.good())
{
//write text to the file
output << "Text To File" << endl;
}
//check that goodbit is set
check_stream_state1(output);
check_stream_state2(output);
output.close();
check_stream_state1(output);
check_stream_state2(output);
fstream input("stream.txt", ios::in);
string text;
//using member function eof()
while(!input.eof())
{
getline (input, text);
cout << text << endl;
}
input.clear();
check_stream_state1(input);
check_stream_state2(input);
input.clear();
input.seekg(0,ios::beg);
int number = 0;
output.close();
return 0; }