SlideShare a Scribd company logo
Unit 5
Template & File
17/10/2016 Jitendra R. Patil 1
Template
• Templates in C++ programming allows function or class to
work on more than one data type at once without writing
different codes for different data types.
• Templates are often used in larger programs for the purpose
of code reusability and flexibility of program.
• The concept of templates can be used in two different
ways:
Function Templates
Class Templates
17/10/2016 Jitendra R. Patil 2
Function Templates
Syntax:
template <class T>
T some_function(T arg)
{
//body of function ;
}
17/10/2016 Jitendra R. Patil 3
//Sorting of number
using Function
Template
#include<iostream.h>
#include<conio.h>
template<class T>
void sort(T a [ ])
{
T temp;
int i , j;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i] > a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"Sorted
array="<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
}
void main()
{
int a[5],i , j;
float b[5];
char c[5];
clrscr();
cout<<"Enter 5 integer
element";
for(i=0;i<5;i++)
cin>>a[i];
cout<<"Enter 5
decimal element";
for(i=0;i<5;i++)
cin>>b[i];
cout<<"Enter 5
character";
for(i=0;i<5;i++)
cin>>c[i];
sort(a);
sort(b);
sort(c);
getch();
}
17/10/2016 Jitendra R. Patil 4
17/10/2016 Jitendra R. Patil 5
#include <iostream>
using namespace std;
// One function works for all data types. This would work
// even for user defined types if operator '>' is overloaded
template <typename T>
T myMax(T x, T y)
{
return (x > y)? x: y;
}
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax for int
cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double
cout << myMax<char>('g', 'e') << endl; // call myMax for char
return 0;
}
Class Templates
template <class type>
class class-name
{
.
.
.
}
17/10/2016 Jitendra R. Patil 6
#include<iostream.h>
#include<conio.h>
template<class T>
class swap
{
T temp,a,b;
public:
void get(T x,T y)
{
a=x;
b=y;
}
void show()
{
temp=a;
a=b;
b=temp;
cout<<"a="<< a<<endl;
cout<<"b="<< b<<endl;
} };
void main()
{
swap<int> p;
clrscr();
p.get(4,9);
swap<float> q;
q.get(2.8,7.6);
p.show();
q.show();
getch();
}
17/10/2016 Jitendra R. Patil 7
File
• File. The information / data stored under a specific name on a storage
device, is called a file.
• Stream. It refers to a sequence of bytes.
• Text file. It is a file that stores information in ASCII characters. In text
files, each line of text is terminated with a special character known as
EOL (End of Line) character or delimiter character.
• Binary file. It is a file that contains information in the same format as it
is held in memory. In binary files, no delimiters are used for a line and
no translations occur here.
17/10/2016 Jitendra R. Patil 8
Files
• It is a process of storing data into files.
• Files are used to store data in a relatively permanent form, on floppy
disk, hard disk, tape or other form of secondary storage.
• Files can hold huge amounts of data if need be. Ordinary variables
(even records and arrays) are kept in main memory which is
temporary and rather limited in size.
17/10/2016 Jitendra R. Patil 9
Why Use Files
• Convenient way to deal with large quantities of data.
• Store data permanently (until file is deleted).
• Avoid having to type data into program multiple times.
• Share data between programs.
17/10/2016 Jitendra R. Patil 10
PROGRAM
DEVICES OR
FILES
Input
Stream
>>
Output
Stream
<<
Data
Data
istream class ostream class
(Insertion
operator)
(Extraction
operator)
Flow of Data
I/O in C++
Different streams are used to represent different kinds of data flow.
Each stream is associated with a particular class, which contains
 member functions and
 definitions for dealing with that particular kind of data flow.
17/10/2016 Jitendra R. Patil 12
The following classes in C++ have access to
file input and output functions:
•Ifstream
Stream class to write on files
•ofstream
Stream class to read from files
•fstream
Stream class to both read and write from/to files.
17/10/2016 Jitendra R. Patil 13
Different file operations
Opening a File:
• OPENING FILE USING CONSTRUCTOR
ofstream outFile("sample.txt"); //output only
ifstream inFile(“sample.txt”); //input only
• OPENING FILE USING open()
Stream-object.open(“filename”, mode)
• ofstream outFile;
outFile.open("sample.txt");
ifstream inFile;
inFile.open("sample.txt");
17/10/2016 Jitendra R. Patil 14
• All these flags can be combined using the bitwise operator OR (|). For
example, if we want to open the file example.bin in binary mode to
add data we could do it by the following call to member function
open():
• fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
17/10/2016 Jitendra R. Patil 15
Mode Flag Description
ios::app Append mode. All output to that file to be appended to the end.
ios::ate Open a file for output and move the read/write control to the end
of the file.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::trunc If the file already exists, its contents will be truncated before
opening the file.
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.
• Following is the standard syntax for close() function, which is a
member of fstream, ifstream, and ofstream objects.
outFile.close();
inFile.close();
17/10/2016 Jitendra R. Patil 16
INPUT AND OUTPUT OPERATION
put() and get() function
the function put() writes a single character to the associated stream. Similarly, the
function get() reads a single character form the associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
17/10/2016 Jitendra R. Patil 17

More Related Content

What's hot

Datastrucure
DatastrucureDatastrucure
Datastrucure
Mohamed Essam
 
File Management in C
File Management in CFile Management in C
File Management in C
Munazza-Mah-Jabeen
 
File Handling
File HandlingFile Handling
File Handling
TusharBatra27
 
Day 4( magic camp)
Day 4( magic camp)Day 4( magic camp)
Day 4( magic camp)
FatemehJamshidi5
 
Files in C
Files in CFiles in C
Files in C
Prabu U
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
Munazza-Mah-Jabeen
 
Getting started with R
Getting started with RGetting started with R
File Types in Data Structure
File Types in Data StructureFile Types in Data Structure
File Types in Data Structure
Prof Ansari
 
Server side scripting language
Server side scripting languageServer side scripting language
Server side scripting language
Girmachew Tilahun
 
Data wrangling with dplyr
Data wrangling with dplyrData wrangling with dplyr
Data wrangling with dplyr
C. Tobin Magle
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
Prerna Sharma
 
31cs
31cs31cs
31cs
Sireesh K
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
keeeerty
 
Reproducible research
Reproducible researchReproducible research
Reproducible research
C. Tobin Magle
 
file management in c language
file management in c languagefile management in c language
file management in c language
chintan makwana
 
Example of Using R #1: Exporting the Result of Correspondence Analysis
Example of Using R #1: Exporting the Result of Correspondence AnalysisExample of Using R #1: Exporting the Result of Correspondence Analysis
Example of Using R #1: Exporting the Result of Correspondence Analysis
khcoder
 
C programming disk file reading and writing
C programming disk file reading and writingC programming disk file reading and writing
C programming disk file reading and writing
rishi ram khanal
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
File Handling - N K Upadhyay
File Handling - N K UpadhyayFile Handling - N K Upadhyay
File Handling - N K Upadhyay
Dipayan Sarkar
 

What's hot (19)

Datastrucure
DatastrucureDatastrucure
Datastrucure
 
File Management in C
File Management in CFile Management in C
File Management in C
 
File Handling
File HandlingFile Handling
File Handling
 
Day 4( magic camp)
Day 4( magic camp)Day 4( magic camp)
Day 4( magic camp)
 
Files in C
Files in CFiles in C
Files in C
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
File Types in Data Structure
File Types in Data StructureFile Types in Data Structure
File Types in Data Structure
 
Server side scripting language
Server side scripting languageServer side scripting language
Server side scripting language
 
Data wrangling with dplyr
Data wrangling with dplyrData wrangling with dplyr
Data wrangling with dplyr
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
31cs
31cs31cs
31cs
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
Reproducible research
Reproducible researchReproducible research
Reproducible research
 
file management in c language
file management in c languagefile management in c language
file management in c language
 
Example of Using R #1: Exporting the Result of Correspondence Analysis
Example of Using R #1: Exporting the Result of Correspondence AnalysisExample of Using R #1: Exporting the Result of Correspondence Analysis
Example of Using R #1: Exporting the Result of Correspondence Analysis
 
C programming disk file reading and writing
C programming disk file reading and writingC programming disk file reading and writing
C programming disk file reading and writing
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File Handling - N K Upadhyay
File Handling - N K UpadhyayFile Handling - N K Upadhyay
File Handling - N K Upadhyay
 

Similar to Unit 5

VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
Abdullah khawar
 
Working with the IFS on System i
Working with the IFS on System iWorking with the IFS on System i
Working with the IFS on System i
Chuck Walker
 
File management
File managementFile management
File management
sumathiv9
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Osmania University
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
Automating API Documentation
Automating API DocumentationAutomating API Documentation
Automating API Documentation
Selvakumar T S
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
lakshmanarao027MVGRC
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
Hatem Abd El-Salam
 
Overview of the DITA Open Toolkit
Overview of the DITA Open ToolkitOverview of the DITA Open Toolkit
Overview of the DITA Open Toolkit
Suite Solutions
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 
Python Course.docx
Python Course.docxPython Course.docx
Python Course.docx
AdnanAhmad57885
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
sudhakargeruganti
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
Hemantha Kulathilake
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
Mukesh Tekwani
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
Papu Kumar
 

Similar to Unit 5 (20)

VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
Working with the IFS on System i
Working with the IFS on System iWorking with the IFS on System i
Working with the IFS on System i
 
File management
File managementFile management
File management
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
Automating API Documentation
Automating API DocumentationAutomating API Documentation
Automating API Documentation
 
File_Handling in C.ppt
File_Handling in C.pptFile_Handling in C.ppt
File_Handling in C.ppt
 
C- language Lecture 8
C- language Lecture 8C- language Lecture 8
C- language Lecture 8
 
Overview of the DITA Open Toolkit
Overview of the DITA Open ToolkitOverview of the DITA Open Toolkit
Overview of the DITA Open Toolkit
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Python Course.docx
Python Course.docxPython Course.docx
Python Course.docx
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 

More from S.S.B.T’s. College of Engineering & Technology

Unit 4
Unit 4Unit 4
Unit 3
Unit 3Unit 3
Unit 2
Unit 2Unit 2
Unit 1
Unit 1Unit 1
Unit 5
Unit 5Unit 5
Unit 4
Unit 4Unit 4
Unit 3
Unit 3Unit 3
Unit 2
Unit 2Unit 2
Unit 1
Unit 1Unit 1

More from S.S.B.T’s. College of Engineering & Technology (9)

Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 1
Unit 1Unit 1
Unit 1
 

Recently uploaded

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 

Recently uploaded (20)

Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 

Unit 5

  • 1. Unit 5 Template & File 17/10/2016 Jitendra R. Patil 1
  • 2. Template • Templates in C++ programming allows function or class to work on more than one data type at once without writing different codes for different data types. • Templates are often used in larger programs for the purpose of code reusability and flexibility of program. • The concept of templates can be used in two different ways: Function Templates Class Templates 17/10/2016 Jitendra R. Patil 2
  • 3. Function Templates Syntax: template <class T> T some_function(T arg) { //body of function ; } 17/10/2016 Jitendra R. Patil 3
  • 4. //Sorting of number using Function Template #include<iostream.h> #include<conio.h> template<class T> void sort(T a [ ]) { T temp; int i , j; for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i] > a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } cout<<"Sorted array="<<endl; for(i=0;i<5;i++) cout<<a[i]<<endl; } void main() { int a[5],i , j; float b[5]; char c[5]; clrscr(); cout<<"Enter 5 integer element"; for(i=0;i<5;i++) cin>>a[i]; cout<<"Enter 5 decimal element"; for(i=0;i<5;i++) cin>>b[i]; cout<<"Enter 5 character"; for(i=0;i<5;i++) cin>>c[i]; sort(a); sort(b); sort(c); getch(); } 17/10/2016 Jitendra R. Patil 4
  • 5. 17/10/2016 Jitendra R. Patil 5 #include <iostream> using namespace std; // One function works for all data types. This would work // even for user defined types if operator '>' is overloaded template <typename T> T myMax(T x, T y) { return (x > y)? x: y; } int main() { cout << myMax<int>(3, 7) << endl; // Call myMax for int cout << myMax<double>(3.0, 7.0) << endl; // call myMax for double cout << myMax<char>('g', 'e') << endl; // call myMax for char return 0; }
  • 6. Class Templates template <class type> class class-name { . . . } 17/10/2016 Jitendra R. Patil 6
  • 7. #include<iostream.h> #include<conio.h> template<class T> class swap { T temp,a,b; public: void get(T x,T y) { a=x; b=y; } void show() { temp=a; a=b; b=temp; cout<<"a="<< a<<endl; cout<<"b="<< b<<endl; } }; void main() { swap<int> p; clrscr(); p.get(4,9); swap<float> q; q.get(2.8,7.6); p.show(); q.show(); getch(); } 17/10/2016 Jitendra R. Patil 7
  • 8. File • File. The information / data stored under a specific name on a storage device, is called a file. • Stream. It refers to a sequence of bytes. • Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. • Binary file. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here. 17/10/2016 Jitendra R. Patil 8
  • 9. Files • It is a process of storing data into files. • Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. • Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main memory which is temporary and rather limited in size. 17/10/2016 Jitendra R. Patil 9
  • 10. Why Use Files • Convenient way to deal with large quantities of data. • Store data permanently (until file is deleted). • Avoid having to type data into program multiple times. • Share data between programs. 17/10/2016 Jitendra R. Patil 10
  • 11. PROGRAM DEVICES OR FILES Input Stream >> Output Stream << Data Data istream class ostream class (Insertion operator) (Extraction operator) Flow of Data
  • 12. I/O in C++ Different streams are used to represent different kinds of data flow. Each stream is associated with a particular class, which contains  member functions and  definitions for dealing with that particular kind of data flow. 17/10/2016 Jitendra R. Patil 12
  • 13. The following classes in C++ have access to file input and output functions: •Ifstream Stream class to write on files •ofstream Stream class to read from files •fstream Stream class to both read and write from/to files. 17/10/2016 Jitendra R. Patil 13
  • 14. Different file operations Opening a File: • OPENING FILE USING CONSTRUCTOR ofstream outFile("sample.txt"); //output only ifstream inFile(“sample.txt”); //input only • OPENING FILE USING open() Stream-object.open(“filename”, mode) • ofstream outFile; outFile.open("sample.txt"); ifstream inFile; inFile.open("sample.txt"); 17/10/2016 Jitendra R. Patil 14
  • 15. • All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open(): • fstream file; file.open ("example.bin", ios::out | ios::app | ios::binary); 17/10/2016 Jitendra R. Patil 15 Mode Flag Description ios::app Append mode. All output to that file to be appended to the end. ios::ate Open a file for output and move the read/write control to the end of the file. ios::in Open a file for reading. ios::out Open a file for writing. ios::trunc If the file already exists, its contents will be truncated before opening the file.
  • 16. 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. • Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects. outFile.close(); inFile.close(); 17/10/2016 Jitendra R. Patil 16
  • 17. INPUT AND OUTPUT OPERATION put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file.get(ch); file.put(ch); write() and read() function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj)); 17/10/2016 Jitendra R. Patil 17