SlideShare a Scribd company logo
Programming Fundametanls
- Moodser Hussain
1
Table of Contents
File:................................................................................................................................................................1
Types of Files:................................................................................................................................................1
File Access Methods:.....................................................................................................................................1
Streams: ........................................................................................................................................................2
Types of Streams:......................................................................................................................................2
Pre-defined stream objects: .....................................................................................................................2
Stream class Hierarchy:.............................................................................................................................2
Opening Files:............................................................................................................................................3
Default opening modes:............................................................................................................................3
Verify File Open:........................................................................................................................................4
Closing file:................................................................................................................................................4
Writing Data to Files I/O: ..........................................................................................................................4
Detecting End-of-File: ...............................................................................................................................4
File:
A file is collection of records and a record contains details about an
entity. A file can be used to store any kind of data. Files are stored
on secondary storage (permanent storage). Normally, data is stored in
variables in a program (which is not a permanent source of storage). The
data has to be entered each time the program get started, that waste a
lot of time of user.
A data file can be used to provide input to a program. It can also be
used to store the output of a program permanently.
Types of Files:
C++ can process two types of files that depends on how data is stored
in files:
• Text File: Contains readable and printable data.
• Binary File: Contains non-readable binary code. Consists of binary
code that is converted by the compiler from source file.
File Access Methods:
The way in which a file can be accessed. It depends on the manner in
which data is stored in the files. File access methods are:
Programming Fundametanls
- Moodser Hussain
2
• Sequential Access Methods: Used to access data in same sequence in
which it was stored. This method, read and write data in a sequence.
In order to read last record, it has to read all records stored
before the last one. It is simplest method to organize files.
Length of each record is not fixed. This method takes less storage,
and more time for searching a record.
• Random Access Method: To access any data item directly without
accessing preceding data. Does not read/write data in sequence.
Much faster than sequential access method. It store data in fixed
length records. It wastes memory storage. As records length is
fixed, it is easy to search the records (simple add # of bytes).
• DBMS store data in Random access format.
Streams:
Series of bytes associated with a file. Contains a data that is being
transferred from one location to another. Each stream is associated with
a specific file using open operation. The information can be exchanged
between program and file, once the file is opened.
Types of Streams:
Two streams are input and output. The streams automatically establish
when a program start execution. The user can use them to input and output
data at any time.
• Standard Input Stream: Used to input data. Establish a connection
between the standard input device and a program. Reading data from
an input stream is called extraction ‘>>’.
• Standard Output Stream: Used to output data. Establish a connection
between the standard output device and a program. Act of writing
data to an output stream is called insertion ‘<<’.
Pre-defined stream objects:
• cin
• cout
• cerr: Occur immediately, un-buffered, used to show errors.
• clog: similary to ‘cerr’ but buffered (means output will not be
displayed until buffer is full)
Stream class Hierarchy:
• ofstream: used to write on file
• ifstream: used to read from file
• fstream: used for read/write (both) from/to file
Programming Fundametanls
- Moodser Hussain
3
these streams are derived directly/indirectly from istream and ostream.
Opening Files:
A file should be opened before it can be processed. A file pointer is
declared and associated with the file (to be opened). A file can be
opened using ifstream, ofstream or fstream. The object is then associated
with a real file. An open file is represented by a stream object. Any
input/output performed to this object will be performed on real file.
fstream file;
open(file_name, mode);
Modes are as follow:
• ios::in -> open a file for input operations
• ios::out -> open a file for output operations
• ios::binary -> open a file in binary mode
• ios::ate -> used to set the initial position at the end of the file
• ios::app -> used to perform all output operations at the end of
file, appending the content at the end of file
• ios::trunc -> delete the previous contents of a file
• ios::nocrate -> used to open file only if it exists
• ios::noreplace -> used to open file only if the file does not exist
otherwise open fails
All of the operations can be combined using pipe sign (|).
Default opening modes:
• ofstream->ios::out
• ifstream->ios::in
• fstream->ios::in|ios::out
Programming Fundametanls
- Moodser Hussain
4
Verify File Open:
A function is_open() is used to verify either a file is open or not. It
takes no parameter and return true if file is open.
if(!(file.is_open()))
cout << “File not open”;
Closing file:
When input/output operations are performed, we can close the file so
that the file can be used by other processes/programs.
file.close();
Writing Data to Files I/O:
file << “This data is entered by C++ Program”;
Detecting End-of-File:
if(file.eof())

More Related Content

What's hot

File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Akhil Kaushik
 
C files
C filesC files
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
Rico
 
Python-File handling-slides-pkt
Python-File handling-slides-pktPython-File handling-slides-pkt
Python-File handling-slides-pkt
Pradyumna Tripathy
 
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
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
yndaravind
 
Fileorganization AbnMagdy
Fileorganization AbnMagdyFileorganization AbnMagdy
Fileorganization AbnMagdy
Mohamed Magdy
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
koolkampus
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
nitamhaske
 
Stream
StreamStream
python file handling
python file handlingpython file handling
python file handling
jhona2z
 
File access methods.54
File access methods.54File access methods.54
File access methods.54
myrajendra
 
Comp102 lec 11
Comp102   lec 11Comp102   lec 11
Comp102 lec 11
Fraz Bakhsh
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
eShikshak
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
Hemantha Kulathilake
 
Fundamental file structure concepts &amp; managing files of records
Fundamental file structure concepts &amp; managing files of recordsFundamental file structure concepts &amp; managing files of records
Fundamental file structure concepts &amp; managing files of records
Devyani Vaidya
 
Windows
WindowsWindows
Windows
Ezzah
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
keeeerty
 
Record storage and primary file organization
Record storage and primary file organizationRecord storage and primary file organization
Record storage and primary file organization
Jafar Nesargi
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 

What's hot (20)

File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
C files
C filesC files
C files
 
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
 
Python-File handling-slides-pkt
Python-File handling-slides-pktPython-File handling-slides-pkt
Python-File handling-slides-pkt
 
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
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Fileorganization AbnMagdy
Fileorganization AbnMagdyFileorganization AbnMagdy
Fileorganization AbnMagdy
 
11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS11. Storage and File Structure in DBMS
11. Storage and File Structure in DBMS
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
Stream
StreamStream
Stream
 
python file handling
python file handlingpython file handling
python file handling
 
File access methods.54
File access methods.54File access methods.54
File access methods.54
 
Comp102 lec 11
Comp102   lec 11Comp102   lec 11
Comp102 lec 11
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Fundamental file structure concepts &amp; managing files of records
Fundamental file structure concepts &amp; managing files of recordsFundamental file structure concepts &amp; managing files of records
Fundamental file structure concepts &amp; managing files of records
 
Windows
WindowsWindows
Windows
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
Record storage and primary file organization
Record storage and primary file organizationRecord storage and primary file organization
Record storage and primary file organization
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 

Similar to 8. file handling

Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
AbdulghafarStanikzai
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
NiharikaDubey17
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
Papu Kumar
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
nishant874609
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
WondimuBantihun1
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
sulekha24
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
HalaiHansaika
 
Data file handling
Data file handlingData file handling
Data file handling
Saurabh Patel
 
File Handling in CPP Programming Language.pptx
File Handling in CPP Programming Language.pptxFile Handling in CPP Programming Language.pptx
File Handling in CPP Programming Language.pptx
SajjadAbdullah4
 
Learn about the File Concept in operating systems ppt
Learn about the File Concept in operating systems pptLearn about the File Concept in operating systems ppt
Learn about the File Concept in operating systems ppt
geethasenthil2706
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
Tanmay Baranwal
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
Sunil Patel
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
YourHelper1
 
SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012
Rian Yulian
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Osmania University
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
YourHelper1
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
MikialeTesfamariam
 

Similar to 8. file handling (20)

Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
File handling4.pdf
File handling4.pdfFile handling4.pdf
File handling4.pdf
 
Filesin c++
Filesin c++Filesin c++
Filesin c++
 
Data file handling
Data file handlingData file handling
Data file handling
 
File Handling in CPP Programming Language.pptx
File Handling in CPP Programming Language.pptxFile Handling in CPP Programming Language.pptx
File Handling in CPP Programming Language.pptx
 
Learn about the File Concept in operating systems ppt
Learn about the File Concept in operating systems pptLearn about the File Concept in operating systems ppt
Learn about the File Concept in operating systems ppt
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012SANS Windows Artifact Analysis 2012
SANS Windows Artifact Analysis 2012
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 

More from Lahore Garrison University

Tree in discrete structure
Tree in discrete structureTree in discrete structure
Tree in discrete structure
Lahore Garrison University
 
Sigmund freud
Sigmund freudSigmund freud
Application of de
Application of deApplication of de
Application of de
Lahore Garrison University
 
Firm practice management
Firm practice managementFirm practice management
Firm practice management
Lahore Garrison University
 
Mapping functions
Mapping functionsMapping functions
Mapping functions
Lahore Garrison University
 
Project dld
Project dldProject dld
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
Lahore Garrison University
 
Ak
AkAk
Ternary operator
Ternary operatorTernary operator
Ternary operator
Lahore Garrison University
 
Bussines plan (maira)
Bussines plan (maira)Bussines plan (maira)
Bussines plan (maira)
Lahore Garrison University
 
Project# 1
Project# 1Project# 1
Shares &amp; stock market
Shares &amp; stock marketShares &amp; stock market
Shares &amp; stock market
Lahore Garrison University
 
Ozone layer research paper (1)
Ozone layer research paper (1)Ozone layer research paper (1)
Ozone layer research paper (1)
Lahore Garrison University
 
Introduction to information and communication technology
Introduction to information and communication technologyIntroduction to information and communication technology
Introduction to information and communication technology
Lahore Garrison University
 
Ict
IctIct
Forty rulesoflove review writing
Forty rulesoflove review writingForty rulesoflove review writing
Forty rulesoflove review writing
Lahore Garrison University
 
Semester 1 exercise for punctuation mark
Semester 1 exercise for punctuation markSemester 1 exercise for punctuation mark
Semester 1 exercise for punctuation mark
Lahore Garrison University
 
Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)
Lahore Garrison University
 
Reading skill
Reading skill Reading skill
Quiz
Quiz Quiz

More from Lahore Garrison University (20)

Tree in discrete structure
Tree in discrete structureTree in discrete structure
Tree in discrete structure
 
Sigmund freud
Sigmund freudSigmund freud
Sigmund freud
 
Application of de
Application of deApplication of de
Application of de
 
Firm practice management
Firm practice managementFirm practice management
Firm practice management
 
Mapping functions
Mapping functionsMapping functions
Mapping functions
 
Project dld
Project dldProject dld
Project dld
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
Ak
AkAk
Ak
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
Bussines plan (maira)
Bussines plan (maira)Bussines plan (maira)
Bussines plan (maira)
 
Project# 1
Project# 1Project# 1
Project# 1
 
Shares &amp; stock market
Shares &amp; stock marketShares &amp; stock market
Shares &amp; stock market
 
Ozone layer research paper (1)
Ozone layer research paper (1)Ozone layer research paper (1)
Ozone layer research paper (1)
 
Introduction to information and communication technology
Introduction to information and communication technologyIntroduction to information and communication technology
Introduction to information and communication technology
 
Ict
IctIct
Ict
 
Forty rulesoflove review writing
Forty rulesoflove review writingForty rulesoflove review writing
Forty rulesoflove review writing
 
Semester 1 exercise for punctuation mark
Semester 1 exercise for punctuation markSemester 1 exercise for punctuation mark
Semester 1 exercise for punctuation mark
 
Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)Scan apr 8, 2020 (1)
Scan apr 8, 2020 (1)
 
Reading skill
Reading skill Reading skill
Reading skill
 
Quiz
Quiz Quiz
Quiz
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 

8. file handling

  • 1. Programming Fundametanls - Moodser Hussain 1 Table of Contents File:................................................................................................................................................................1 Types of Files:................................................................................................................................................1 File Access Methods:.....................................................................................................................................1 Streams: ........................................................................................................................................................2 Types of Streams:......................................................................................................................................2 Pre-defined stream objects: .....................................................................................................................2 Stream class Hierarchy:.............................................................................................................................2 Opening Files:............................................................................................................................................3 Default opening modes:............................................................................................................................3 Verify File Open:........................................................................................................................................4 Closing file:................................................................................................................................................4 Writing Data to Files I/O: ..........................................................................................................................4 Detecting End-of-File: ...............................................................................................................................4 File: A file is collection of records and a record contains details about an entity. A file can be used to store any kind of data. Files are stored on secondary storage (permanent storage). Normally, data is stored in variables in a program (which is not a permanent source of storage). The data has to be entered each time the program get started, that waste a lot of time of user. A data file can be used to provide input to a program. It can also be used to store the output of a program permanently. Types of Files: C++ can process two types of files that depends on how data is stored in files: • Text File: Contains readable and printable data. • Binary File: Contains non-readable binary code. Consists of binary code that is converted by the compiler from source file. File Access Methods: The way in which a file can be accessed. It depends on the manner in which data is stored in the files. File access methods are:
  • 2. Programming Fundametanls - Moodser Hussain 2 • Sequential Access Methods: Used to access data in same sequence in which it was stored. This method, read and write data in a sequence. In order to read last record, it has to read all records stored before the last one. It is simplest method to organize files. Length of each record is not fixed. This method takes less storage, and more time for searching a record. • Random Access Method: To access any data item directly without accessing preceding data. Does not read/write data in sequence. Much faster than sequential access method. It store data in fixed length records. It wastes memory storage. As records length is fixed, it is easy to search the records (simple add # of bytes). • DBMS store data in Random access format. Streams: Series of bytes associated with a file. Contains a data that is being transferred from one location to another. Each stream is associated with a specific file using open operation. The information can be exchanged between program and file, once the file is opened. Types of Streams: Two streams are input and output. The streams automatically establish when a program start execution. The user can use them to input and output data at any time. • Standard Input Stream: Used to input data. Establish a connection between the standard input device and a program. Reading data from an input stream is called extraction ‘>>’. • Standard Output Stream: Used to output data. Establish a connection between the standard output device and a program. Act of writing data to an output stream is called insertion ‘<<’. Pre-defined stream objects: • cin • cout • cerr: Occur immediately, un-buffered, used to show errors. • clog: similary to ‘cerr’ but buffered (means output will not be displayed until buffer is full) Stream class Hierarchy: • ofstream: used to write on file • ifstream: used to read from file • fstream: used for read/write (both) from/to file
  • 3. Programming Fundametanls - Moodser Hussain 3 these streams are derived directly/indirectly from istream and ostream. Opening Files: A file should be opened before it can be processed. A file pointer is declared and associated with the file (to be opened). A file can be opened using ifstream, ofstream or fstream. The object is then associated with a real file. An open file is represented by a stream object. Any input/output performed to this object will be performed on real file. fstream file; open(file_name, mode); Modes are as follow: • ios::in -> open a file for input operations • ios::out -> open a file for output operations • ios::binary -> open a file in binary mode • ios::ate -> used to set the initial position at the end of the file • ios::app -> used to perform all output operations at the end of file, appending the content at the end of file • ios::trunc -> delete the previous contents of a file • ios::nocrate -> used to open file only if it exists • ios::noreplace -> used to open file only if the file does not exist otherwise open fails All of the operations can be combined using pipe sign (|). Default opening modes: • ofstream->ios::out • ifstream->ios::in • fstream->ios::in|ios::out
  • 4. Programming Fundametanls - Moodser Hussain 4 Verify File Open: A function is_open() is used to verify either a file is open or not. It takes no parameter and return true if file is open. if(!(file.is_open())) cout << “File not open”; Closing file: When input/output operations are performed, we can close the file so that the file can be used by other processes/programs. file.close(); Writing Data to Files I/O: file << “This data is entered by C++ Program”; Detecting End-of-File: if(file.eof())