Files in C++.pdf is the notes of cpp for reference

anuvayalil5525 150 views 14 slides Jan 18, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Notes


Slide Content

Files in C++

Files and Stream classes
Files the collection of records or programs of same nature.
In C++, the concept of stream or stream classes to implement the I/O operations
with the console and disk files.
The I/O devices such as terminals, keyboards, disk etc. supplies an interface to
the programmer for the smooth working with the system. This interface is known
as stream.
A stream is a sequence of bytes. It acts either as a source from which the input
data can be obtained or a destination to which the output data can be sent.
The source stream that provides data to the program is called the input stream
and the destination stream that receives output from the program is known as
output stream.
cinand coutare used earlier in programs to perform this input and output
operations

Files and Stream classes
ios
ostreamstreambufistream
iostream
istream_withassign Iostream_withassign ostream_withassign
Input
Pointer
output

Stream classes for console operations
Class Name Contents
ios Contains all basic facilities of input and output classes
istream get(), getline(), read() and operator such as >>
ostream put() and write(), insertion operator <<
iostream All input and output functions(ios/istream/ostream)
streambuf It acts as a base for filebufclass used in files

Unformatted I/O operations
cin>>var1>>var2>>……>>varn;
cout<<var1<<var2<<……<<varn;
Eg:
put() and get()
char c;
cin.get(c);
char c;
cin.get(c);
cout.put(c);

Unformatted I/O operations
getline() and write()
The getline() reads a whole line of text end with newline character(or when we
press return(enter) key)
cin.getline(line, size);
Eg;
char name[20];
cin.getline(name, 20);
cout.write(line, size);
The write() displays/prints a whole line of text

Classes for file stream operations
ios
ostreamstreambufistream
iostream
ifstream fstream ofstream
Input
Pointer
output
filebuf
fstreambase
iostream.hfile
fstream.hfile

Details of file stream classes
Class Contents
filebuf Set the file buffers to read and write. It contains close() and open() as
members
fstreambase Provides operations common to the file streams, serves the base for
ifsteram, ofstream, fstreamclasses. It contains open() and close() functions
ifstream Provide input operations. Contains open() as default input mode. Inherits
the functions get(), getline(), read(), seekg() and tellg()
ofstream Provide output operations. Contains close() as default output mode. Inherits
the functions put(), write(), seekp() and tellp()
fstream Provides all input and output operations. It performs all functions of
ifstreamand ofstream. Inherits all istreamand ostreamclasses through
iostream

Opening and closing a file
•Suitable name for the file
•Data type structure
•Purpose
•Opening method
•Input.data
•Test.doc
•Student
•output

Opening and closing a file
•Opening method
1.Using the constructor function of the class
Here constructors are used in files to initialize the file stream object.
1.Choose appropriate filestreamobject
2.Initialize the object using suitable file name
ofstreamoutfile(“results”); // this creates outfileas an ofstreamobject that manages the
output stream
This statement also opens the file resultsand attaches it to the output stream outfile
Ifstreaminfile(“input.dat”);
outfile<<“Total”;
outfile<<sum;
infile>> rollno;
outfile.close(); //to close the outfile
infile.close();

Opening and closing a file
•Opening method
1.Using the open() function for opening the file
The function open() can be used to open multiple files that use the same stream object.
file_stream_classstream_object;
Stream_object.open(“filename”);
ofstreamoutfile;
outfile.open(“Data1”);
……………………..
outfile.close();
outfile.open(“Data2”);
……………………………
outfile.close();

File Opening modes
•Opening modes
•It specifies the purpose of opening the file
stream_object.open(“file_name”, mode);
Mode specifies the purpose for which the file is opened.
Default mode for ifstreamis ios::in and for ofstreamis ios::out
Parameter Meaning
ios::app Append to end-of-file
ios::ate Go to the end-of-file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exists
ios::noreplace Open fails if the file exists
ios::out Open files for writing only
ios::trunc Delete contents of the file if it exists

File pointers and their manipulations
Each file has two associated pointers known as file pointers.
One is the input pointer (get pointer) and other one is output pointer
(put pointer)
We are using it for reading and writing the files
seekg() Moves get pointer to a specified position
seekp() Moves the put pointer to a specified position
tellg() Gives the current position of the get pointer
tellp() Gives the current position of the put pointer
infile.seekg(10);

File pointers and their manipulations
Specifying the offset
seekg(offset, refposition);
seekp(offset, refposition);
The parameter offset represents the number of bytes the file pointer is to be moved
from the location specified by the parameter refposition.
Refpositionscan from the following
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file
fout.seekg(0, ios::beg) go to the start
fout.seekg(0, ios::cur)stay at the current position
fout.seekg(0, ios::end)go to the end of the file
fout.seekg(m, ios::beg) go to the m+1th postion
fout.seekg(-m, ios::cur)go backwards by m bytes from the current position
Tags