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

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Kürzlich hochgeladen (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

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