You are on page 1of 49

Programming in C++ using

Data Structures

Unit III
Basic Concepts of Data Structure
Data Structure is a way of collecting and organizing data in such a way that we can perform operations
on these data in an effective way. Data Structures is about rendering data elements in terms of some
relationship, for better organization and storage.
For example, In the data, player's name "Hitesh" and age 26. Here "Hitesh" is of String data type and 26 is
of integer data type.
We can organize this data as a record like Player record. Now we can collect and store player's records
in a file or database as a data structure.
For example: "Gayle" 30, "Sachin" 31, "Parth" 33
In simple language, Data Structures are structures programmed to store ordered data, so that various
operations can be performed on it easily.
Data Definition
Data Definition defines a particular data with following characteristics.
• Atomic − Definition should define a single concept
• Traceable − Definition should be be able to be mapped to some data element.
• Accurate − Definition should be unambiguous.
• Clear and Concise − Definition should be understandable.
Basic Concepts of Data Structure…Cont’d
Data Object
Data Object represents an object having a data.
Basic Concepts of Data Structure…Cont’d

Introduction to Data structures


Data Type
Data type is way to classify various types of data such as integer, string etc. which determines the
values that can be used with the corresponding type of data, the type of operations that can be
performed on the corresponding type of data.
Data type of two types
1. Built-in Data Type
2. Derived Data Type
1. Built-in Data Type

Those data types for which a language has built-in support are known as Built-in Data types. For
example, most of the languages provides following built-in data types.
• Integers
• Boolean (true, false)
• Floating (Decimal numbers)
• Character and Strings
Basic Concepts of Data Structure…Cont’d
2. Derived Data Type
Those data types which are implementation independent as they can be implemented in one or other
way are known as derived data types. These data types are normally built by combination of primary or built-
in data types and associated operations on them. For example
• List
• Array
• Stack
• Queue
Basic Operations
The data in the data structures are processed by certain operations. The data structure chosen largely
depends on the frequency of the operation that needs to be performed on the data structure.
• Traversing
• Searching
• Insertion
• Deletion
• Sorting
• Merging
Basic Concepts of Data Structure…Cont’d
Types Of Data Structures
Data Structures are broadly classified into 2 types.
1. Primitive data structures
2. Non-primitive data structures
Basic Concepts of Data Structure…Cont’d
1. Primitive Data Structures
Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions. They have different representations on different computers. Integers, Floating point
numbers, Character constants, String constants and Pointers come under this category.
2. Non-primitive Data Structures
Non-primitive data structures are more complicated data structures and are derived from primitive
data structures. They emphasize on grouping same or different data items with relationship between
each data item. Arrays, Lists and Files come under this category.
Asymptotic Notations
Asymptotic notations are method used to estimate and represent the efficiency of an algorithm using
simple formula. This ca be useful for separating algorithms that leads to different amounts of work for
large inputs.
Following are commonly used asymptotic notations used in calculating running time complexity of an
algorithm.
• Ο Notation
• Ω Notation
• θ Notation
Basic Concepts of Data Structure…Cont’d

Big 'O' Notation


The Ο(n) is the formal way to express the upper
bound of an algorithm's running time. It measures the
worst case time complexity or longest amount of time an
algorithm can possibly take to complete.
For a Function f(n):-
f(n)) = {g(n) : there exists c > 0 and n0 such that g(n) ≤
c.f(n) for all n > n0.}
Basic Concepts of Data Structure…Cont’d

Omega(Ω) Notation
The Ω(n) is the formal way to express the lower bound
of an algorithm's running time. It measures the best case
time complexity or best amount of time an algorithm can
possibly take to complete.
for a function f(n):-
Ω(f(n)) ≥ {g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0.}
Basic Concepts of Data Structure…Cont’d

Theta(θ) Notation
The θ(n) is the formal way to express both the lower
bound and upper bound of an algorithm's running time.
for a function f(n):-
θ(f(n)) = {g(n) if and only if g(n) = Ο(f(n)) and g(n) =
Ω(f(n)) for all n > n0.}
Arrays
An array is a collection of similar items stored in
contiguous memory locations. In programming,
sometimes a simple variable is not enough to hold all
the data. For example, let’s say we want to store the
marks of 500 students, having 500 different variables
for this task is not feasible, we can define an array
with size 500 that can hold the marks of all students.
Array Representation
Following are the important terms to understand the
concept of Array.
• Element − Each item stored in an array is called an
element.
• Index − Each location of an element in an array has
a numerical index, which is used to identify the
element.
Arrays…Cont’d
Declaring and Accessing Array Elements
Array index starts with 0, which means the first array element is at index 0, second is at index 1
and so on. We can use this information to display the array elements.
Arrays can be declared in various ways in different languages. For illustration, let's take C++ array
declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C++ array
declaration.
Arrays…Cont’d
As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at index 6 as 9.
Basic Operations
Following are the basic operations supported by an array.
• Traverse − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
Arrays…Cont’d
Traverse Operation: This operation is to traverse through the elements of an array.
Example
Following program traverses and prints the elements of an array:
#include <iostream.h>
main()
{
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
cout << "The original array elements are :\n"; Output:
for(i = 0; i<n; i++) The original array elements are :
{ LA[0] = 1
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[1] = 3
LA[2] = 5
} LA[3] = 7
} LA[4] = 8
Arrays…Cont’d

Insertion Operation: Insert operation is to insert one or more data elements into an array. Based on the requirement, a new
element can be added at the beginning, end, or any given index of array.
Here, we see a practical implementation of insertion operation, where we add data at the end of the array

#include <iostream.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
Output:
cout << "The original array elements are :\n";
The original array elements are :
for(i = 0; i<n; i++) {
LA[0] = 1
cout << "LA[“ << i << “ ] = “ << LA[i] << endl;
LA[1] = 3
}
LA[2] = 5
n = n + 1;
LA[3] = 7
while( j >= k) {
LA[4] = 8
LA[j+1] = LA[j]; The array elements after insertion :
j = j - 1; LA[0] = 1
} LA[1] = 3
LA[k] = item; LA[2] = 5
cout << "The array elements after insertion :\n"; LA[3] = 10
for(i = 0; i<n; i++) { LA[4] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[5] = 8
}
}
Arrays…Cont’d

Deletion Operation: Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
Here is the practical implementation of delete an element available at the Kth position of LA. (Consider LA is a linear array with
N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; Output:
} The original array elements are :
j = k; LA[0] = 1
while( j < n) { LA[1] = 3
LA[2] = 5
LA[j-1] = LA[j];
LA[3] = 7
j = j + 1;
LA[4] = 8
}
The array elements after deletion :
n = n -1; LA[0] = 1
cout << "The array elements after deletion :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[3] = 8
}
}
Arrays…Cont’d

Search Operation: You can perform a search for an array element based on its value or its index.
Here is the practical implementation to find an element with a value of ITEM using sequential search. (Consider
LA is a linear array with N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
cout << "The original array elements are :\n";
for(i = 0; i<n; i++) {
cout << "LA[“ << i << “ ] = “ << LA[i] << endl;
} Output:
while( j < n){
The original array elements are :
LA[0] = 1
if( LA[j] == item ) {
LA[1] = 3
break;
LA[2] = 5
}
LA[3] = 7
j = j + 1; LA[4] = 8
} Found element 5 at position 3
cout << "Found element ” << item << “at position ” << j+1;
}
Arrays…Cont’d

Update Operation: Update operation refers to updating an existing element from the array at a given
index.
Here is the practical implementation to update an element available at the Kth position of LA. (Consider
LA is a linear array with N elements and K is a positive integer such that K<=N.)
#include <iostream.h>
void main() {
int LA[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10; Output:
int i, j; The original array elements are :
LA[0] = 1
cout << "The original array elements are :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 5
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[3] = 7
LA[4] = 8
}
The array elements after updation :
LA[k-1] = item; LA[0] = 1
cout << "The array elements after updation :\n"; LA[1] = 3
for(i = 0; i<n; i++) { LA[2] = 10
LA[3] = 7
cout << "LA[“ << i << “ ] = “ << LA[i] << endl; LA[4] = 8
}
}
Arrays…Cont’d

Types of array
Array can be single or multidimensional. The number of subscript or index determines the dimensions of
the array. An array of one dimension is known as a one-dimensional array or 1-D array, while an array of two
dimensions is known as a two-dimensional array or 2-D array
One-dimensional array:
Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another.
Syntax: datatype array_name[size];
datatype: It denotes the type of the elements in the array.
array_name: Name of the array. It must be a valid identifier.
Size: Number of elements an array can hold.
Two dimensional array:
In two dimensional array, elements are stored as row and column.
Syntax: datatype array_name[size1][size2];
datatype: It denotes the type of the elements in the array.
array_name: Name of the array. It must be a valid identifier.
size1: Number of rows in an array.
Size2: Number of columns in an array.
Arrays…Cont’d

Two dimensional array Declaration:


int arr[2][3];
Initialization:
We can initialize the array in many ways:
Method 1:
int arr[2][3] = {10, 11 ,12 ,20 ,21 , 22};
Method 2:
This way of initializing is preferred as you can visualize the rows and columns here.
int arr[2][3] = {{10, 11 ,12} , {20 ,21 , 22}};
Accessing array elements:
arr[0][0] – first element
arr[0][1] – second element
arr[0][2] – third element
arr[1][0] – fourth element
arr[1][1] – fifth element
arr[1][2] – sixth element
Arrays…Cont’d

Three dimensional array Declaration:


int arr[2][3][2];
Initialization:
We can initialize the array in many ways:
Method 1:
int arr[2][3][2] = {1, -1 ,2 ,-2 , 3 , -3, 4, -4, 5, -5, 6, -6};
Method 2:
This way of initializing is preferred as you can visualize the rows and columns here.
int arr[2][3][2] = {
{ {1,-1}, {2, -2}, {3, -3}},
{ {4, -4}, {5, -5}, {6, -6}}
}
Arrays…Cont’d

Multidimensional array:
Multidimensional arrays are also known as array of arrays. The data in multidimensional array is
stored in a tabular form as shown in the diagram below:
Arrays…Cont’d

Advantages of an Array
1. Random access of elements using array index.
2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.
Disadvantages of an Array
6. Allows a fixed number of elements to be entered which is decided at the time of declaration.
Unlike a linked list, an array is not dynamic.
7. Insertion and deletion of elements can be costly since the elements are needed to be managed
in accordance with the new memory allocation.
Ordered list

An ordered list is a list in which the order of the items is significant. However, the items in
an ordered list are not necessarily sorted. Consequently, it is possible to change the order of
items and still have a valid ordered list.

Consider a list of the titles of the chapters in this book. The order of the items in the list
corresponds to the order in which they appear in the book. However, since the chapter titles
are not sorted alphabetically, we cannot consider the list to be sorted. Since it is possible to
change the order of the chapters in book, we must be able to do the same with the items of
the list. As a result, we may insert an item into an ordered list at any position.
Working with Files
The I/O system of C++ handles file operations which are very much similar to the console input and
output operations. It uses file streams as an interface between the program and the files. The stream that
supplies data to the program is known as input stream and the one that receives data from the program is
known as output stream. In the other words, the input stream extracts(read) data from the file and the
output stream inserts (writes) data to the file.

The input operation involves the creation of an input stream and linking it with the program and the
input file. Similarly, the output operation involves establishing an output stream with necessary links with
the program and the output file.
Working with Files…Cont’d

Classes for file Stream operations:


The I/O system of C++ contains a set of classes that defines the file handling methods. These include
ifstream, ofstream and fstream. These classes are derived from fstreambase and form the corresponding
iostream class. These classes ,designed to manage the disk files, are declared in fstream and therefore we
must include this file in any program that uses files.

.
Working with Files…Cont’d

Details of File stream Classes

Class Contents
Its purpose is to set the file buffers to read and write. Contains Openprot
filebuf
constant used in the open() of file stream classes. Also contain close() and
open() as members.
fstreambase Provides operations common to file streams. Serves as a base for fstream,
ifstream and ofstream class. Contains open() and close() functions.
Provides input operations. Contains open() with default input mode.
.ifstream Inherits the functions get(), getline(), read(), seekg(), tellg() functions from
istream.
ofstream Provides output operations. Contains open() with default output mode.
Inherits put(), seekp(), tellp() and write() functions from ostream.
Provides support for simultaneous input and output operations. Contains
fstream
open() with default input mode. Inherits all the functions from istream and
ostream classes through iostream.
Working with Files…Cont’d
Opening and closing a file:
If we want to use a disk file, we need to decide the following about the file and its intended use.
1. Suitable name for the file
2. Data type and structure
3. Purpose
4. Opening Method
The filename is a string of characters that makeup a valid filename for the operating system. It may
contain two parts, a primary name and optional period with extension.
Examples: Input.data Test.doc Student
For opening a file, we must first create a file stream and then linked it to the filename. A file stream can
be defined using the classes ifstream, ofstream and fstream that are contained in the header file fstream.
The class to be used depends upon the purpose, that is, whether we want to read data from the file or write
data to it. A file can be opened in two ways
1. Using the constructor function of the class.
2. Using the member function open() of the class.
The first method is useful when we use only one file in the stream. The second method is used when we
want to manage multiple files using one stream.
Working with Files…Cont’d
Opening Files using Constructor:
While using constructor for opening file, filename is used to initialize the file stream object. This involves the
following steps
1. Create a file stream object to manage the stream using the appropriate class. That is to say, the
class ofstream is used to create the output stream and the class ifstream to create the input
stream.
2. Initialize the file object with the desired file name.
For example, the following statement opens a file named “results” for output:
ofstream outfile(“results”); //output only
This create outfile as an ofstream object that manages the output stream. This object can beany valid
C++ name. This statement also opens the file results and attaches it to the output stream outfile.
Working with Files…Cont’d
Similarly, the following statement declares infile as an ifstream object and attaches it to the file data for
reading (input).
ifstream infile(“data”); //input only
The program may contain statements like
outfile << “Total”:
outfile << sum;
infile >> number;
infile >> string;
The same file name can be used for both reading and writing data as shown below.
Working with Files…Cont’d
Program1
……………….
……………….
ofstream outfile (“salary”); //creates outfile and connects salary to it
………………
………………
Program 2
………………
………………
ifstream infile (“salary”); //creates infile and connects salary to it
………………..
…………………

The connection with a file is closed automatically when the stream object expires(when a program
terminates). In the above statement, when the program1 is terminated, the salary file is disconnected from
the outfile stream. Similar action takes place when the program 2 terminates.
Working with Files…Cont’d
Instead of using two programs, one for writing data and another for reading data ,a single program can
be used to do both operations on a file.
…………
…………….
outfile.close(); //disconnect salary from outfile
ifstream infile (“salary”); // and connect to infile
………….
……………
infile.close(); // Disconnect salary from infile

Although we have uses a single program, we created two files stream objects, outfile and infile.
Working with Files…Cont’d
Opening Files using Open():
The function open() can be used to open multiple files that use the same stream object. For example to
process a set of files sequentially. in such cases, a single stream object can be created and can be used to
open each file in turn. This can be done as follows;
File-stream-class stream-object;
stream-object.open (“filename”);
Example:
ofstream outfile; // Create stream (for output)
outfile.open(“DATA1”) // Connect stream to DATA1
………………….
………………….
outfile.close(); // Disconnect stream from DATA1
Outfile.open(“DATA2); // Connect stream to DATA2
…………………
…………………
outfile.close(); // Disconnect stream from DATA2
…………………
…................
Above program segment opens two files in sequence for writing data. Note that the first file closes before
opening the second one.
Working with Files…Cont’d

Working with Multiple files while (fin) // Check end-of-file


//Creating files with open() function {
#include <iostream.h> fin.getline(line, N); // read a line
#include<fstream.h> cout<<line; // display it
int main() }
{ fin.close(); // disconnect “country”
ofstream fout; // Create output stream fin.open(“capital”); // connect “capital”
fout.open(“country”); // connect “country” to it cout<<”\nContents of capital file \n”;
fout<<”United states of America \n”; while(fin)
fout<<”United Kingdom\n”; {
fout<<”South korea\n”; fin.getline(line, N);
fout.close(); // disconnect “country” cout<<line; Output:
fout.open(“capital”); // connect “capital” } Contents of country file
fout<<”Washington\n”; fin.close(); United states of America
fout<<”London\n”; return 0; United Kingdom
fout<<”Seoul \n”; } South korea
fout.close(); // disconnect “capital”
// Reading the files Contents of capital file
const int N =80; // size of line Washington
char line[N]; London
ifstream fin; // Create input stream Seoul
fin.open(“country”); // Connect “country” to it
cout<<”Contents of country file \n”;
Working with Files…Cont’d

Detecting end-of-file:
Detection of the end-of-file condition is necessary for preventing any further attempt to read data from
the file. An ifstream object, such as fin, returns a value of 0 if any error occurs in the file operation including
the end-of-file condition. Thus, the while loop terminates when fin returns a value of zero on reaching the
end-of –file condition.
There is an another approach to detect the end-of-file condition. In the below statement
if(fin1.eof() !=0 )
{
exit(1);
}
eof() is a member function of ios class. It returns a non zero value if end-of-file(EOF) condition is
encountered, and a zero, otherwise. Therefore, the above statement terminates the program on reaching the
end of the file.
Working with Files…Cont’d

File Pointers and Manipulators


Each file has two associated pointers known as file pointers. One of them is called the input pointer(or
get pointers) and the other is called output pointer(or put pointers). The input pointer is used for reading the
contents of a given file location and the output pointer is used for writing to a given file location. Each time
an input or output operation takes place, the appropriate pointer is automatically advanced.
Default actions:
When a file is opened in read-only mode, the input pointer is automatically set at the beginning so that file
can be read from the start. Similarly, when a file is opened in write-only mode, the existing contents are
deleted and the output pointer is set at the beginning. This enables us to write the file from start. In case an
existing file is opened to add more data, the file is opened in ‘append’ mode. This moves the output pointer
to the end of file.
Working with Files…Cont’d

Functions for Manipulations of File pointer


All the actions on the file pointers takes place automatically by default. Following file stream functions
are used for controlling the movement of file pointers
• seekg() Moves get pointer (input)to a specified location.
• seekp() Moves put pointer (output) to a specified location.
• tellg() Give the current position of the get pointer.
• tellp() Give the current position of the put pointer.
For example, the statement
infile.seekg(10);
moves the file pointer to the byte number 10.The bytes in a file are numbered beginning from zero.
Therefore, the pointer will be pointing to the 11th byte in the file.
Consider the following statements:
ofstream fileout;
fileout.open(“hello”, ios::app);
int p=fileout.tellp();
On execution of these statements, the output pointer is moved to the end of file “hello” and the value of p
will represent the number of bytes in the file.
Working with Files…Cont’d

Specifying the offset


We can move a file pointer to a desired position using the ‘seek’ function. The argument to these
functions represents the absolute position in the file as shown below

‘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 is to be moved from the location
specified by the parameter refposition. The refposition takes one of the following three constants defined in
the ios class:
• ios::beg Start of file
• ios::cur Current position of the pointer
• ios::end End of file
Working with Files…Cont’d

The seekg() function moves the associated file’s ‘get’ pointer while the seekp() function moves the
associated file’s ‘put‘ pointer. The following listshows some sample pointer offset calls and their actions. fout
is an ofstream object.
Seek call Action
fout.seekg(o,ios::beg) Go to start
fout.seekg(o,ios::cur) Stay at the current position
fout.seekg(o,ios::end) Go to the end of file
fout.seekg(m,ios::beg) Move to (m+1)th byte in the file
fout.seekg(m,ios::cur) Go forward by m byte from current position
fout.seekg(-m,ios::cur) Go backward by m bytes from current position.
fout.seekg(-m,ios::end) Go backward by m bytes from the end
Working with Files…Cont’d

Sequential Input and Output Operations


The file stream classes support a number of member functions for performing the input and output
operations on files. One pair of functions, put() and get() are designed for handling a single character at a
time. Another pair of functions, write() and read() are designed to write and read blocks of binary data.
put() and get() Functions:
The function put() writes a single character to the associated stream. Similarly, the function get() reads
a single character from the associated stream.
The following program illustrates how these functions work on a file. The program requests for a
string. On receiving the string, the program writes it, character by character, to the file using the put()
function in a for loop. The length of string is used to terminate the for loop.
The program then displays the contents of file on the screen. It uses the function get() to fetch a
character from the file and continues to do so until the end –of –file condition is reached. The character
read from the files is displayed on screen using the operator <<.
Working with Files…Cont’d

I/O Operations on Characters while(file)


#include <iostream.h> {
#include<fstream.h> file.get(ch); // get a character from file
#include<string.h> cout<<ch; //Display it on screen
}
int main() return 0;
{ }
char string[80];
cout<<”Enter a string : “;
cin>>string; Output:
int len=strlen(string); Enter a string: C++_Programming
fstream file; //Input/output stream
cout<<“\nOpening the ‘TEXT’ file and storing the Opening the ‘TEXT’ file and storing the string in it.
string in it.\n\n”;
file.open(“TEXT”, ios::in | ios::out); Reading the file contents: C++_Programming.
for (int i=0;i<len;i++)
file.put(string[i]); // put a character to file
file .seekg(0); // go to start
char ch;
cout<< “Reading the file contents: “;
Working with Files…Cont’d

write() and read () functions:


The functions write() and read(), unlike the functions put() and get(), handle the data in binary form.
This means that the values are stored in the disk file in same format in which they are stored in the internal
memory. The following figure shows how an int value 2594 is stored in the binary and character formats.

An int takes two bytes to store its value in the binary form, irrespective of its size. But a 4 digit int will take
four bytes to store it in the character form.
The binary input and output functions takes the following form:
infile.read (( char * ) & V, sizeof (V));
outfile.write (( char *) & V, sizeof (V));
These functions take two arguments. The first is the address of the variable V, and the second is the length
of that variable in bytes. The address of the variable must be cast to type char*(i.e pointer to character
type).The following program illustrates how these two functions are used to save an array of floats numbers
and then recover them for display on the screen.
Working with Files…Cont’d

I/O Operations on Binary Files for (i=0;i<4;i++)


#include <iostream.h> {
#include<fstream.h> cout.setf(ios::showpoint);
#include<string.h> cout<<setw(10)<<setprecision(2)<<height[i];
}
const char * filename =”Binary”; infile.close();
return 0;
int main() }
{
float height[4] ={ 175.5,153.0,167.25,160.70}; Output:
ofstream outfile; 175.50 153.00 167.25
outfile.open(filename);
outfile.write((char *) & height, sizeof(height));
outfile.close(); //Close the file for reading
for (int i=0;i<4;i++) //Clear array from memory
height[i]=0;
ifstream infile;
infile.open(filename);
infile.read ((char *) & height, sizeof (height));
Working with Files…Cont’d

Updating a file:
Updating is a routine task in the maintenance of any data file. The updating would include one or more of the
following tasks.
• Displaying the contents of a file
• Modifying an existing item
• Adding a new item
• Deleting an existing item
These actions requires the file pointers to move to a particular location that corresponds to the item/object
under consideration. This can be easily implemented if the file contains a collection of items/objects of equal lengths.
In such cases the size of the object can be obtained using the statement
int object_length = sizeof (Object);
Then, the location of a desired object, say the m’th object, may be obtained as follows
int location = m * object_length;
The location gives the byte number of the first byte of the m’th object. Now, we can set the file pointer to reach
this byte with the help of seekg() or seekp()
We can also find out the total number of objects in a file using the object_length as follows
int n = file_size/object_length;
The file size can be obtained using the function tellg() or tellp() when the file pointer is located at the end of the
file.
Working with Files…Cont’d

Error Handling during File Operations


So far we have been opening and using the files for reading and writing on the assumption that
everything is fine with the files. This may not be true always. For instance, one of the following things may
happen when dealing with the files.
1. A file which we are attempting to open for reading does not exist.
2. The file name used for a new file may already exist.
3. We are attempting an invalid operation such as reading past the end-of-file.
4. There may not be any space in the disk for storing more data.
5. We may use an invalid file name.
6. We may attempt to perform an operation when the file is not opened for that purpose.
The C++ file stream inherits a ‘stream-state ‘member from the class ios. This member records
information on the status of a file that is being currently used. The stream state member uses bit fields to
store the status of the error conditions stated above.
The class ios support several member functions that can be used to read the status recorded in a file
stream. These functions along with their meanings are listed in the below table
Working with Files…Cont’d

Error handling functions


Function Return value and meaning
eof() Returns true(non zero value) if end of file is encountered while
reading otherwise returns false(zero).
fail() Returns true when an input or output operation has failed .
bad() Returns true if an invalid operation is attempted or any
unrecoverable error has occurred. However, if it is false, it may be
possible to recover from any other error reported and continues
operation.
good() Returns true if no error has occurred. This means all the above
functions are false. For instance, if file.good() is true, all is well with
the stream file and we can proceed to perform I/O operations.
When it returns false, no further operations is carried out.

These functions may be used in the appropriate places in a program to locate the status of a file
stream and thereby to take the necessary corrective measures.
Working with Files…Cont’d
Command Line Arguments
Like C,C++ also support a feature that facilitates the supply of arguments to the main() function. These
arguments are supplied at the time of invoking the program. They are typically used to pass the names of
data files. Example:
C > exam data results
Here, exam is the name of file containing the program to be executed, and data and results are the
filenames passed to program as command line arguments.
The command line arguments are typed by the user and are delimited by a space. The first argument is
always the filename(command name) and contains the program to be executed.
The main() functions which have been using up to now without any argument can take two arguments
as shown below:
main(int argc, char * argv[])
The first argument argc (known as argument counter) represents the number of arguments in
command line. The second argument argv (known as argument vector) is an array of char type pointers
that points to the command-line arguments. The size of this array will be equal to the value of argc. For
instance, for command line
C>exam data results
Command Line Arguments…Cont’d
The value of argc would be 3 and the argv would be an array of three pointers to string as shown
below:
argv[0] ---> exam
argv[1] ---> data
argv[2] ---> results
Note that argv[0] always represents the command name that invokes the program. The character
pointer argv[1] and argv[2] can be used as file names in the file opening statements as shown below:
…………
…………
inline.open(argv[1]); //open data file for reading
…………..
…………..
outfile.open(argv[2]); //open result file for writing
…………..
…………..
End of Unit III

You might also like