0% found this document useful (0 votes)
5 views25 pages

19. Advanced C++ Programming - Files and Streams

The document provides an overview of file handling in C++ using the fstream library, which includes classes for reading (ifstream), writing (ofstream), and both (fstream) to files. It explains how to open, close, read from, and write to files, along with the relevant functions and modes for file operations. Additionally, it covers stream positioning functions to manage the read and write positions within a file.

Uploaded by

vinnoval123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

19. Advanced C++ Programming - Files and Streams

The document provides an overview of file handling in C++ using the fstream library, which includes classes for reading (ifstream), writing (ofstream), and both (fstream) to files. It explains how to open, close, read from, and write to files, along with the relevant functions and modes for file operations. Additionally, it covers stream positioning functions to manage the read and write positions within a file.

Uploaded by

vinnoval123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Advanced C++

Programming
C++ Files and Streams
What is fstream library?

C++ provides the following classes to perform output and


input of characters to/from a FILE:

🞆 ofstream: Stream class to write on files


🞆 ifstream: Stream class to read from files
🞆 fstream: Stream class to both read and write from/to files.

To perform file processing in C++, header files <iostream> and


<fstream> must be included in your C++ source file.
OPENING A FILE:

🞆 A file must be opened before you can read from it or write to it.
Either the ofstream or fstream object may be used to open a
file for writing and ifstream object is used to open a file for
reading purpose only.

🞆 This is the function member open of stream classes:

void open(const char *filename, ios::openmode mode);


OPENING A FILE:

void open(const char *filename, ios::openmode mode);

This is a string representing the


name and location of the file to
be opened
It is an optional parameter with a
combination of the following flags
<in next slide>
OPENING A FILE:

void open(const char *filename, ios::openmode mode);


Open Mode Functions
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::binary Open in binary mode.
Set the initial position at the end of the file.
ios::ate
If this flag is not set, the initial position is the beginning of the file.
All output operations are performed at the end of the file, appending the content to the
ios::app
current content of the file.
If the file is opened for output operations and it already existed, its previous content is
ios::trunc
deleted and replaced by the new one.
OPENING A FILE: EXAMPLE

🞆 Syntax of opening a file with a mode(writing mode):


ofstream outfile;
outfile.open("file.txt", ios::out);
🞆 You can combine two or more of these values by ORing them
together.
🞆 Syntax of opening a file with two or more modes(reading and
writing mode):

fstream afile;
afile.open("file.txt", ios::out | ios::in );
OPENING A FILE: EXAMPLE

🞆 If you don’t specify the mode, each class has a default mode.

class default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out

🞆 Example is when you open a file:


ofstream outfile;
outfile.open("file.txt");
OPENING A FILE:

🞆 To check if a file stream was successful opening a file, you can


do it by calling to member is_open(). This member function
returns a bool value of true in the case that indeed the stream
object is associated with an open file, or false otherwise:
CLOSING A FILE:

🞆 When a C++ program terminates it automatically closes flushes


all the streams, release all the allocated memory and close all
the opened files. But it is always a good practice that a
programmer should close all the opened files before program
termination.

🞆 We will call the stream's member function close(). This member


function takes flushes the associated buffers and closes the file:
CLOSING A FILE:

🞆 This close function is a member of fstream, ifstream, and


ofstream objects.

void close();
🞆 Following is an example for the standard syntax for close()
function:
ofstream outfile;
outfile.open("file.txt");
outfile.close();
WRITING TO A FILE:

🞆 While doing C++ programming, you write information to a file


from your program using the stream insertion operator (<<)
just as you use that operator to output information to the
screen. The only difference is that you use
an ofstream or fstream object instead of the cout object.
WRITING TO A FILE: EXAMPLE

Writing operations on text files are


performed in the same way we
operated with cout:

OUTPUT FILE:
OTHER FUNCTIONS OF WRITING
CHARACTERS TO A FILE :

🞆 Writing single character to file


void put (char c);

🞆 Writing array of chacters with specified number of n


void write (const char* str, int n);
READING TO A FILE:

🞆 You read information from a file into your program using the
stream extraction operator (>>) just as you use that operator to
input information from the keyboard. The only difference is that
you use an ifstream or fstream object instead of the cin
object.
READING TO A FILE: EXAMPLE #1

Reading from a file can also be


performed in the same way that
we did with cin: (but reading stops
at whitespaces)
OTHER FUNCTIONS OF READING
CHARACTERS TO A FILE :
🞆 Extract single character

char get();
🞆 Extract characters in a line

istream& getline (char* str, streamsize n, char delim = ‘\n’);


🞆 Extract C-string

istream& get (char* str, int n, char delim = ‘\n’);


Extract characters into str until n-1 characters are not
extracted or delim character is not met.
READING TO A FILE: EXAMPLE #2

Example of using getline():


<it was able to read including
whitespaces>
WRITING AND READING TO A FILE:
EXAMPLE
WRITING AND READING TO A FILE:

OUTPUT FILE: READ FILE IN CONSOLE:


GET AND PUT STREAM
POSITIONING:

🞆 Stream class can determine the position of the streams in the


file.
🞆 You can access the positioning functions if you use the certain
class as follows: <next slide, I will explain each functions>
Class Functions availability

ofstream tellp() and seekp() only


ifstream tellg() and seekg() only
fstream tellp(), tellg(), seekp() and seekg()
GET STREAM POSITIONING:

🞆 These two member functions with no parameters return a value


of the member type streampos, which is a type representing the
current get position (in the case of tellg) or the put position (in
the case of tellp). <“\n” or newline is counted>

Functions Description
returns the current reading
int tellg();
position
returns the current writing
int tellp();
position
GET STREAM POSITIONING:
EXAMPLE

Note: that <“\n” or


newline is counted>
PUT STREAM POSITION:

🞆 These functions allow to change the location of the get and put
positions. Both functions are overloaded with two different
prototypes. The first form is: <“\n” or newline is counted>

Functions Description Position Description


void Changing current read Points at the beginning of
ios::beg
seekg(position); position. stream.
void Changing current write Points at the current
ios::cur
seekp(position); position. position.
Points at the end of
ios::end
stream.
PUT STREAM POSITIONING:
EXAMPLE

Previous string was


overwritten…

Note: that <“\n” or


newline is counted>
ANOTHER EXAMPLE: TELL AND SEEK

You might also like