SlideShare ist ein Scribd-Unternehmen logo
1 von 36
File Input and Output


Objectives
In this lesson, you will learn to:
 Define the stream class hierarchy
 Identify the stream insertion and extraction operators
 Use the stream classes for file input and output
 Differentiate between text and binary file input and
  output
 Apply the following functions for opening and closing
  files:
     open()
     close()

©NIIT                                 OOPS/Lesson 12/Slide 1 of 36
File Input and Output


Objectives (Contd.)
 Use the open mode bits
 Randomly access data files




©NIIT                          OOPS/Lesson 12/Slide 2 of 36
File Input and Output


Stream Class Hierarchy

                             System-Defined
                  ios
                             Streams

istream                   ostream


               iostream

                                User-Defined
ifstream       fstream    ofstream Streams


©NIIT                     OOPS/Lesson 12/Slide 3 of 36
File Input and Output


Features of Stream Classes
Stream Classes:
 Form a powerful set of classes that can be modified,
  extended, or expanded to incorporate user-defined
  data types or classes
 Are fully buffered to reduce disk access
 Encapsulate their internal working from the user
 Offer a rich set of error-handling facilities




©NIIT                                  OOPS/Lesson 12/Slide 4 of 36
File Input and Output


Stream Insertions
 Are output operations for which the functions are
  defined in the ostream class




©NIIT                               OOPS/Lesson 12/Slide 5 of 36
File Input and Output


Stream Insertion Operators
 Are defined in the ostream class
 The operator “” is called the inserter




©NIIT                                OOPS/Lesson 12/Slide 6 of 36
File Input and Output


Stream Extractions
 Are input operations for which the functions are
  defined in the istream class




©NIIT                               OOPS/Lesson 12/Slide 7 of 36
File Input and Output


Stream Extraction Operators
 Are defined in the istream class and are used to
  receive data from the input device
 The operator “”, called the extractor, accepts any
  built-in data type passed as arguments




©NIIT                               OOPS/Lesson 12/Slide 8 of 36
File Input and Output


The get() and getline() Functions
 Are used to read a complete string from the input
stream
 Recognize white spaces
 The get() function
   Syntax:
   cin.get(char *str, int len, char delim =
                  'n');
 The getline() function
   Syntax:
   cin.getline(char *str, int len, char
       delim = 'n');

©NIIT                                  OOPS/Lesson 12/Slide 9 of 36
File Input and Output


Problem Statement 10.D.1
Write an application that will accept input from the user
by using the get() function until the input from the
keyboard is “N” or nine characters depending on
whichever is first.




©NIIT                                OOPS/Lesson 12/Slide 10 of 36
File Input and Output


File Input and Output Using Built-in Data Types
 Integer Input and Output
  Example:
  #include fstream
  int main()
  {
        ofstream outobj(INT.TST);
        outobj  25  ' '  4567  ' ' 
        8910;
        return 0;
  }
©NIIT                         OOPS/Lesson 12/Slide 11 of 36
File Input and Output


File Input and Output Using Built-in Data Types
(Contd.)
 Character input and output
   Example:
  #include fstream
   int main()
   {
        ofstream out(STR.TST);
        out  This is a test string;
        return 0;
   }
©NIIT                          OOPS/Lesson 12/Slide 12 of 36
File Input and Output


File Input and Output Using Objects
Example:
#includefstream.h
class student
     {
private:
        int iReg_no;char cName[20];
public:
        void setRegno();
        void setName();
        int getRegno();
        char *getName();
  };

©NIIT                        OOPS/Lesson 12/Slide 13 of 36
File Input and Output


File Input and Output Using Objects (Contd.)
void main()
{
        ofstream Sfil(“studfile.dat”);
        char ch;
        student Svar;
        Svar.setRegno();
        Svar.setName();
        SfilSvar.getRegno()
           “ ”Svar.getName();
        Sfil.close(); //Closes the open file

©NIIT                           OOPS/Lesson 12/Slide 14 of 36
File Input and Output


File Input and Output Using Objects (Contd.)
        cout “n Do you want to view the
              contents of a file (y/n)?”;
        cinch;
        if(ch== ‘y’)
        {
            ifstream Sfil(“studfile.dat”);
            char ireg;char nam[20];
            Sfiliregnam;
            cout“n Registration Number is ”
               ireg;
            cout“n Student Name is ” nam;
        }
}
©NIIT                          OOPS/Lesson 12/Slide 15 of 36
File Input and Output


Binary Input and Output
 Character input and output
        The put() function
        Syntax:
        [object_of_ofstream].put(character_variable);
        The get() function
        Syntax:
        [object_of_ifstream].get(character_variable);
 Integer input and output
        The read function
        Syntax:
        read(char* addr, int size)
©NIIT                                 OOPS/Lesson 12/Slide 16 of 36
File Input and Output


Binary Input and Output (Contd.)
     The write function
        Syntax:
         write(char* addr, int size)
 File input and output using abstract data types
     read() and write() functions are used to read
      or write user-defined objects on a file




©NIIT                                  OOPS/Lesson 12/Slide 17 of 36
File Input and Output


The open() Function
 Is used to open a file
  Example:
  ifstream Ifil;      //creates an
                //unopened input stream
  Ifil.open(DATA.DAT);   //associates
                //the stream to a file




©NIIT                      OOPS/Lesson 12/Slide 18 of 36
File Input and Output


The close() Function
 Is used to close a file
  Example:
  ofstream Ofil;
  Ofil.open(DATA.DAT);
  ...
  ...
  Ofil.close();




©NIIT                       OOPS/Lesson 12/Slide 19 of 36
File Input and Output


Problem Statement 10.D.2
As part of the customer tracking system for Diaz
Telecommunication, Inc., an application is to be created
that can:
 Accept customer details
 Save the customer details in a file
 Display the details of existing customers




©NIIT                               OOPS/Lesson 12/Slide 20 of 36
File Input and Output


Problem Statement 10.P.1
As part of the reservation system for the Northern
Railways, an application is to be created that:
 Accepts passenger details
 Saves passenger details in a file
 Displays the details of the passengers who have
  confirmed bookings




©NIIT                                 OOPS/Lesson 12/Slide 21 of 36
File Input and Output


Open Mode Bits
 Are defined in the ios class
 Are bits that are associated with the opening of files
 Represent the mode in which the file is opened




©NIIT                               OOPS/Lesson 12/Slide 22 of 36
File Input and Output


Open Mode Bits (Contd.)



Mode                           Explanation

app     Starts reading or writing at the end of the file. Does not have a
        meaning with input streams, it is used only for output streams
in      Open for reading
out     Open for writing
ate     Seek to the end of the file
trunc   If the file exists, it is truncated, i.e. all data is erased before
        writing/reading




©NIIT                                          OOPS/Lesson 12/Slide 23 of 36
File Input and Output


The get Pointer
 Specifies the location in the file where the next read
  operation will occur




©NIIT                              OOPS/Lesson 12/Slide 24 of 36
File Input and Output


The put Pointer
 Specifies the location in the file where the next write
  operation will occur




©NIIT                              OOPS/Lesson 12/Slide 25 of 36
File Input and Output


The seekg() Function
 Helps to control the get pointer
 Moves the get pointer to an absolute address within
  the file or to a certain number of bytes from a
  particular position
 Takes two arguments:
   The number of bytes to move

    The reference in the file from where the pointer
     has to be repositioned
        Example:
        ifstream iFil;
        iFil.seekg(10,ios::beg);
©NIIT                                OOPS/Lesson 12/Slide 26 of 36
File Input and Output


The tellg() Function
 Helps to control the get pointer
 Can be used to find the current position of the get file
  pointer in a file
 Does not take any arguments
                      Example:
                 int
   iPosition=iFil.tellg();




©NIIT                                OOPS/Lesson 12/Slide 27 of 36
File Input and Output


The seekp() Function
 Helps to control the put pointer
 Moves the put pointer to an absolute address within
  the file or to a certain number of bytes from a
  particular position




©NIIT                                OOPS/Lesson 12/Slide 28 of 36
File Input and Output


The tellp() Function
 Helps to control the put pointer
 Can be used to find the current position of the put file
  pointer in a file




©NIIT                                OOPS/Lesson 12/Slide 29 of 36
File Input and Output


Problem Statement 10.D.3
Write an application that reads the contents of any
existing text file in reverse order.




©NIIT                               OOPS/Lesson 12/Slide 30 of 36
File Input and Output


Problem Statement 10.P.2
Modify the Northern Railway application defined in
10.P.1 for the user to:
 Query the passenger details based on PNR Number
 Modify the booking status, if the reservation is
  cancelled




©NIIT                               OOPS/Lesson 12/Slide 31 of 36
File Input and Output


Summary
In this lesson, you learned that:
 The information can be saved permanently on the disk
  using either flat-files or a database
 A file has two components:
     Fields
     Records
 A field is the smallest unit of data that contains only
  one type of data
 A record is a collection of all the fields that present
  information for a single entity uniquely identified by a
  key
©NIIT                                OOPS/Lesson 12/Slide 32 of 36
File Input and Output


Summary (Contd.)
 The file is also defined as a stream of characters or a
  flow of related data and are two kinds:
     Output streams, which allow you to write or store
      characters
     Input streams, which allow you to read or fetch
      characters
 The stream class consists mainly of the following
  classes:
     ios
     istream
     ostream
©NIIT                               OOPS/Lesson 12/Slide 33 of 36
File Input and Output


Summary (Contd.)
     iostream
     ifstream
     ofstream
     fstream
 Inserters and extractors do not recognize white
  spaces
 Inserters and extractors can be used for both input
  and output of all the built-in and user-defined objects
 File streams consists of their own constructors and
  destructors

©NIIT                               OOPS/Lesson 12/Slide 34 of 36
File Input and Output


Summary (Contd.)
 File streams consists of the open() and
  close()member functions
 Binary input and output is supported by the following
  stream class member functions:
     get()
     put()
     getline()
     read()
     write()


©NIIT                              OOPS/Lesson 12/Slide 35 of 36
File Input and Output


Summary (Contd.)
 The C++ - I/O system manages two integer values
  associated with file positions:
     The get pointer
     The put pointer
 Random access of data files can be done using the
  seekg() and seekp() member functions
 The current positions of the get and put pointers in a
  file can be found by the tellg() and tellp()
  functions, respectively



©NIIT                              OOPS/Lesson 12/Slide 36 of 36

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (17)

List moderate
List   moderateList   moderate
List moderate
 
C++ oop
C++ oopC++ oop
C++ oop
 
Rust and Eclipse
Rust and EclipseRust and Eclipse
Rust and Eclipse
 
Oops concept
Oops conceptOops concept
Oops concept
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
 
C++ classes
C++ classesC++ classes
C++ classes
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Srgoc dotnet
Srgoc dotnetSrgoc dotnet
Srgoc dotnet
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
Savitch Ch 06
Savitch Ch 06Savitch Ch 06
Savitch Ch 06
 
03. oop concepts
03. oop concepts03. oop concepts
03. oop concepts
 

Ähnlich wie Aae oop xp_12

Ähnlich wie Aae oop xp_12 (20)

Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Oops recap
Oops recapOops recap
Oops recap
 
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
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
Cis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential filesCis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential files
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
working with files
working with filesworking with files
working with files
 
Z OS IBM Utilities
Z OS IBM UtilitiesZ OS IBM Utilities
Z OS IBM Utilities
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Unit 5
Unit 5Unit 5
Unit 5
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
file handling c++
file handling c++file handling c++
file handling c++
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
 

Mehr von Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Kürzlich hochgeladen

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Aae oop xp_12

  • 1. File Input and Output Objectives In this lesson, you will learn to: Define the stream class hierarchy Identify the stream insertion and extraction operators Use the stream classes for file input and output Differentiate between text and binary file input and output Apply the following functions for opening and closing files: open() close() ©NIIT OOPS/Lesson 12/Slide 1 of 36
  • 2. File Input and Output Objectives (Contd.) Use the open mode bits Randomly access data files ©NIIT OOPS/Lesson 12/Slide 2 of 36
  • 3. File Input and Output Stream Class Hierarchy System-Defined ios Streams istream ostream iostream User-Defined ifstream fstream ofstream Streams ©NIIT OOPS/Lesson 12/Slide 3 of 36
  • 4. File Input and Output Features of Stream Classes Stream Classes: Form a powerful set of classes that can be modified, extended, or expanded to incorporate user-defined data types or classes Are fully buffered to reduce disk access Encapsulate their internal working from the user Offer a rich set of error-handling facilities ©NIIT OOPS/Lesson 12/Slide 4 of 36
  • 5. File Input and Output Stream Insertions Are output operations for which the functions are defined in the ostream class ©NIIT OOPS/Lesson 12/Slide 5 of 36
  • 6. File Input and Output Stream Insertion Operators Are defined in the ostream class The operator “” is called the inserter ©NIIT OOPS/Lesson 12/Slide 6 of 36
  • 7. File Input and Output Stream Extractions Are input operations for which the functions are defined in the istream class ©NIIT OOPS/Lesson 12/Slide 7 of 36
  • 8. File Input and Output Stream Extraction Operators Are defined in the istream class and are used to receive data from the input device The operator “”, called the extractor, accepts any built-in data type passed as arguments ©NIIT OOPS/Lesson 12/Slide 8 of 36
  • 9. File Input and Output The get() and getline() Functions Are used to read a complete string from the input stream Recognize white spaces The get() function Syntax: cin.get(char *str, int len, char delim = 'n'); The getline() function Syntax: cin.getline(char *str, int len, char delim = 'n'); ©NIIT OOPS/Lesson 12/Slide 9 of 36
  • 10. File Input and Output Problem Statement 10.D.1 Write an application that will accept input from the user by using the get() function until the input from the keyboard is “N” or nine characters depending on whichever is first. ©NIIT OOPS/Lesson 12/Slide 10 of 36
  • 11. File Input and Output File Input and Output Using Built-in Data Types Integer Input and Output Example: #include fstream int main() { ofstream outobj(INT.TST); outobj 25 ' ' 4567 ' ' 8910; return 0; } ©NIIT OOPS/Lesson 12/Slide 11 of 36
  • 12. File Input and Output File Input and Output Using Built-in Data Types (Contd.) Character input and output Example: #include fstream int main() { ofstream out(STR.TST); out This is a test string; return 0; } ©NIIT OOPS/Lesson 12/Slide 12 of 36
  • 13. File Input and Output File Input and Output Using Objects Example: #includefstream.h class student { private: int iReg_no;char cName[20]; public: void setRegno(); void setName(); int getRegno(); char *getName(); }; ©NIIT OOPS/Lesson 12/Slide 13 of 36
  • 14. File Input and Output File Input and Output Using Objects (Contd.) void main() { ofstream Sfil(“studfile.dat”); char ch; student Svar; Svar.setRegno(); Svar.setName(); SfilSvar.getRegno() “ ”Svar.getName(); Sfil.close(); //Closes the open file ©NIIT OOPS/Lesson 12/Slide 14 of 36
  • 15. File Input and Output File Input and Output Using Objects (Contd.) cout “n Do you want to view the contents of a file (y/n)?”; cinch; if(ch== ‘y’) { ifstream Sfil(“studfile.dat”); char ireg;char nam[20]; Sfiliregnam; cout“n Registration Number is ” ireg; cout“n Student Name is ” nam; } } ©NIIT OOPS/Lesson 12/Slide 15 of 36
  • 16. File Input and Output Binary Input and Output Character input and output The put() function Syntax: [object_of_ofstream].put(character_variable); The get() function Syntax: [object_of_ifstream].get(character_variable); Integer input and output The read function Syntax: read(char* addr, int size) ©NIIT OOPS/Lesson 12/Slide 16 of 36
  • 17. File Input and Output Binary Input and Output (Contd.) The write function Syntax: write(char* addr, int size) File input and output using abstract data types read() and write() functions are used to read or write user-defined objects on a file ©NIIT OOPS/Lesson 12/Slide 17 of 36
  • 18. File Input and Output The open() Function Is used to open a file Example: ifstream Ifil; //creates an //unopened input stream Ifil.open(DATA.DAT); //associates //the stream to a file ©NIIT OOPS/Lesson 12/Slide 18 of 36
  • 19. File Input and Output The close() Function Is used to close a file Example: ofstream Ofil; Ofil.open(DATA.DAT); ... ... Ofil.close(); ©NIIT OOPS/Lesson 12/Slide 19 of 36
  • 20. File Input and Output Problem Statement 10.D.2 As part of the customer tracking system for Diaz Telecommunication, Inc., an application is to be created that can: Accept customer details Save the customer details in a file Display the details of existing customers ©NIIT OOPS/Lesson 12/Slide 20 of 36
  • 21. File Input and Output Problem Statement 10.P.1 As part of the reservation system for the Northern Railways, an application is to be created that: Accepts passenger details Saves passenger details in a file Displays the details of the passengers who have confirmed bookings ©NIIT OOPS/Lesson 12/Slide 21 of 36
  • 22. File Input and Output Open Mode Bits Are defined in the ios class Are bits that are associated with the opening of files Represent the mode in which the file is opened ©NIIT OOPS/Lesson 12/Slide 22 of 36
  • 23. File Input and Output Open Mode Bits (Contd.) Mode Explanation app Starts reading or writing at the end of the file. Does not have a meaning with input streams, it is used only for output streams in Open for reading out Open for writing ate Seek to the end of the file trunc If the file exists, it is truncated, i.e. all data is erased before writing/reading ©NIIT OOPS/Lesson 12/Slide 23 of 36
  • 24. File Input and Output The get Pointer Specifies the location in the file where the next read operation will occur ©NIIT OOPS/Lesson 12/Slide 24 of 36
  • 25. File Input and Output The put Pointer Specifies the location in the file where the next write operation will occur ©NIIT OOPS/Lesson 12/Slide 25 of 36
  • 26. File Input and Output The seekg() Function Helps to control the get pointer Moves the get pointer to an absolute address within the file or to a certain number of bytes from a particular position Takes two arguments: The number of bytes to move The reference in the file from where the pointer has to be repositioned Example: ifstream iFil; iFil.seekg(10,ios::beg); ©NIIT OOPS/Lesson 12/Slide 26 of 36
  • 27. File Input and Output The tellg() Function Helps to control the get pointer Can be used to find the current position of the get file pointer in a file Does not take any arguments Example: int iPosition=iFil.tellg(); ©NIIT OOPS/Lesson 12/Slide 27 of 36
  • 28. File Input and Output The seekp() Function Helps to control the put pointer Moves the put pointer to an absolute address within the file or to a certain number of bytes from a particular position ©NIIT OOPS/Lesson 12/Slide 28 of 36
  • 29. File Input and Output The tellp() Function Helps to control the put pointer Can be used to find the current position of the put file pointer in a file ©NIIT OOPS/Lesson 12/Slide 29 of 36
  • 30. File Input and Output Problem Statement 10.D.3 Write an application that reads the contents of any existing text file in reverse order. ©NIIT OOPS/Lesson 12/Slide 30 of 36
  • 31. File Input and Output Problem Statement 10.P.2 Modify the Northern Railway application defined in 10.P.1 for the user to: Query the passenger details based on PNR Number Modify the booking status, if the reservation is cancelled ©NIIT OOPS/Lesson 12/Slide 31 of 36
  • 32. File Input and Output Summary In this lesson, you learned that: The information can be saved permanently on the disk using either flat-files or a database A file has two components: Fields Records A field is the smallest unit of data that contains only one type of data A record is a collection of all the fields that present information for a single entity uniquely identified by a key ©NIIT OOPS/Lesson 12/Slide 32 of 36
  • 33. File Input and Output Summary (Contd.) The file is also defined as a stream of characters or a flow of related data and are two kinds: Output streams, which allow you to write or store characters Input streams, which allow you to read or fetch characters The stream class consists mainly of the following classes: ios istream ostream ©NIIT OOPS/Lesson 12/Slide 33 of 36
  • 34. File Input and Output Summary (Contd.) iostream ifstream ofstream fstream Inserters and extractors do not recognize white spaces Inserters and extractors can be used for both input and output of all the built-in and user-defined objects File streams consists of their own constructors and destructors ©NIIT OOPS/Lesson 12/Slide 34 of 36
  • 35. File Input and Output Summary (Contd.) File streams consists of the open() and close()member functions Binary input and output is supported by the following stream class member functions: get() put() getline() read() write() ©NIIT OOPS/Lesson 12/Slide 35 of 36
  • 36. File Input and Output Summary (Contd.) The C++ - I/O system manages two integer values associated with file positions: The get pointer The put pointer Random access of data files can be done using the seekg() and seekp() member functions The current positions of the get and put pointers in a file can be found by the tellg() and tellp() functions, respectively ©NIIT OOPS/Lesson 12/Slide 36 of 36