c++day-14
TRANSCRIPT
-
7/31/2019 C++Day-14
1/13
-
7/31/2019 C++Day-14
2/13
Explain Files & its Basics
File modes
Writing data to a file & reading data from a file
Explain File copy
-
7/31/2019 C++Day-14
3/13
A Stream is generally referred to flow of data.
Different streams are used to represent different kinds of data flow
Each stream is associated with particular class which contains
member functions and definitions for dealing with that particular kind
of data flow
The sequence of input bytes are called as Input Stream
The sequence of output bytes are called as Output Stream
The cin object with >>operator is used for getting input
The cout object with
-
7/31/2019 C++Day-14
4/13
ios
istream ostream
iostream
fstreamifstream ofstream
-
7/31/2019 C++Day-14
5/13
The Header File contains the definitions for filestream classes
It contain the definitions for three classes. They are
1) ifstream
2) ofstream
3) fstream
Ofstream :
Every object of type ofstream uses a filename to storeinformation in that file.
Ifstream :To read the contents from a file we must create an object of
class ifstream.
-
7/31/2019 C++Day-14
6/13
Opening a File
The Open() function is used to open different files that uses thesame stream object.
Syntax : open(filename, file mode);File Mode Description
ios :: in Open file for reading
ios :: out Open file for writing
ios :: app Open file for reading and/or writing the data atthe end of file (append)
ios :: ate Erase the file before reading or
writing(Truncated)ios :: nocreate Error when opening, if file does not already
exists
ios :: no replace Error when opening, if file does not alreadyexists, unless ate or app is set
ios :: binary Open file in binary mode (not text mode)
-
7/31/2019 C++Day-14
7/13
Closing a File
To close a file, the member function close() must be used. It takesno parameters and returns no value.
Syntax : close();
Detecting End-Of-File
It is necessary for preventing any attempt to read data from thefile.
Syntax : eof()
put() and get() functions :
put() function displays a single character on the screen.
Syntax : put(char);
get() function gets a single character from the file
Syntax : get(void)
-
7/31/2019 C++Day-14
8/13
#include
#include
#includevoid main()
{
ofstream outfile;
char fname[20];
cout
-
7/31/2019 C++Day-14
9/13
#include#include
#include#includevoid main(){ifstream infile;
char fname[20];char ch;coutfname;
infile.open(fname);if(infile.fail()){cout
-
7/31/2019 C++Day-14
10/13
#include#include
#include#includevoid main(){ofstream outfile;ifstream infile;
char source[10],target[10],ch ;clrscr();coutsource;couttarget;infile.open(source);if(infile.fail()){cout
-
7/31/2019 C++Day-14
11/13
The getline() and write() functions
These two functions are line oriented input and output functions
respectively.
getline() function reads a complete line until it encounters a
newline character.
Syntax : cin.getline(char*,size);
Char* holds the inputline
Size The number of characters to be read
write Function
It is used for displaying a text in a line until it encounters a \n
characters.
Syntax : cout.write(char*,size);
-
7/31/2019 C++Day-14
12/13
getline() and write() function Example
#include#includevoid main(){char name[50];clrscr();
Cout
-
7/31/2019 C++Day-14
13/13
A stream is generally referred to a flow of data.
Each stream is associated with a particular class which contains
member functions and definitions for dealing with that particular
kind of data flow
The sequence of input bytes are called Input stream
The sequence of output bytes are called as output stream
The Ofstream is used for writing information to a file
The ifstream class is used for reading information from a file
The stream class is used for both writing and reading from a file