chapter 9. file and stream

Upload: minhquangel

Post on 06-Jul-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Chapter 9. File and Stream

    1/25

    Chapter 9. STREAMS AND FILES

    Chapter 9. STREAMS AND FILES

    Minh Quang NGUYEN

    Hanoi National University of Education

    September 2015

    Paris Saclay Minh Quang NGUYEN 1

  • 8/17/2019 Chapter 9. File and Stream

    2/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    The ios Class

    The istream Class

    The ostream Class

    The iostream class

    Disk File I/O with Streams

    You do it

    Q & A

    Paris Saclay Minh Quang NGUYEN 2

  • 8/17/2019 Chapter 9. File and Stream

    3/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Introduction

     A stream is a general name given to a flow of data.  In C++ a stream is represented by an object of a particular

    class.

    So far we’ve used the  cin  and  cout  stream objects.

    Paris Saclay Minh Quang NGUYEN 3

  • 8/17/2019 Chapter 9. File and Stream

    4/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Introduction

     A stream is a general name given to a flow of data.  In C++ a stream is represented by an object of a particular

    class.

    So far we’ve used the  cin  and  cout  stream objects.

    Paris Saclay Minh Quang NGUYEN 4

  • 8/17/2019 Chapter 9. File and Stream

    5/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Introduction

     A stream is a general name given to a flow of data.  In C++ a stream is represented by an object of a particular

    class.

    So far we’ve used the  cin  and  cout  stream objects.

    Paris Saclay Minh Quang NGUYEN 5

  • 8/17/2019 Chapter 9. File and Stream

    6/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Advantages of Streams

     They are more simpler than traditional C functions such asprintf()  and  scanf(), and - for files -  fprintf(), fscanf().

     One can overload existing operators and functions, such asthe insertion  () operators, to work withclasses created.

    Paris Saclay Minh Quang NGUYEN 6

  • 8/17/2019 Chapter 9. File and Stream

    7/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Advantages of Streams

     They are more simpler than traditional C functions such asprintf()  and  scanf(), and - for files -  fprintf(), fscanf().

     One can overload existing operators and functions, such asthe insertion  () operators, to work withclasses created.

    Paris Saclay Minh Quang NGUYEN 7

  • 8/17/2019 Chapter 9. File and Stream

    8/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Advantages of Streams

     They are more simpler than traditional C functions such asprintf()  and  scanf(), and - for files -  fprintf(), fscanf().

     One can overload existing operators and functions, such asthe insertion  () operators, to work withclasses created.

    Paris Saclay Minh Quang NGUYEN 8

  • 8/17/2019 Chapter 9. File and Stream

    9/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    Advantages of Streams

    In an environment with a graphical user interface such asWindows, where direct text output to the screen is not used...

    They are the best way to write data to files, and also to formatdata in memory for later use in text input/output windows andother GUI elements.

    Paris Saclay Minh Quang NGUYEN 9

    Ch 9 STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    10/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    What have we known?

     The extraction operator  >>   is a member of the  istream  class,insertion operator  >)

    operators   Theostream  contains  put()  and   write(), and the overloaded

    insertion   (

  • 8/17/2019 Chapter 9. File and Stream

    11/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    What have we known?

     The extraction operator  >>   is a member of the  istream  class,insertion operator  >)

    operators   Theostream  contains  put()  and   write(), and the overloaded

    insertion   (

  • 8/17/2019 Chapter 9. File and Stream

    12/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    What have we known?

     The extraction operator  >>   is a member of the  istream  class,insertion operator  >)

    operators   Theostream  contains  put()  and   write(), and the overloaded

    insertion   (

  • 8/17/2019 Chapter 9. File and Stream

    13/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    What have we known?

     The extraction operator  >>   is a member of the  istream  class,insertion operator  >)

    operators   Theostream  contains  put()  and   write(), and the overloaded

    insertion   (

  • 8/17/2019 Chapter 9. File and Stream

    14/25

    Chapter 9. STREAMS AND FILES

    Stream Classes

    What have we known?

     The extraction operator  >>   is a member of the  istream  class,insertion operator  >)

    operators   Theostream  contains  put()  and   write(), and the overloaded

    insertion   (

  • 8/17/2019 Chapter 9. File and Stream

    15/25

    Chapter 9. STREAMS AND FILES

    The ios Class

    Introduction

      The  ios  class is the granddaddy of all the stream classes

     The three most important features are the formatting flags,the error-status flags, and the file operation mode.

    Paris Saclay Minh Quang NGUYEN 15

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    16/25

    p 9 S S S

    The ios Class

    Formatting Flags

    Formatting flags are a set of enum definitions in ios.

    Paris Saclay Minh Quang NGUYEN 16

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    17/25

    p

    The ios Class

    Formatting Flags

    They are must be preceded with the name  ios  and the

    scope-resolution operator (for example,  ios::skipws).

    cout.setf(ios::left); // left justify output text

    cout >> \This text is left-justified";

    cout.unsetf(ios::left); // return to default (right justified)

    Paris Saclay Minh Quang NGUYEN 17

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    18/25

    The istream Class

    Functions

    The  istream  class, which is derived from  ios, performs

    input-specific activities, or extraction. The functions which aremost commonly used from the  istream  class.

      get()

      get()

      getline()

    Paris Saclay Minh Quang NGUYEN 18

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    19/25

    The ostream Class

    Functions

    The ostream class handles output or insertion activities. The mostcommonly used member functions

    Paris Saclay Minh Quang NGUYEN 19

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    20/25

    The iostream class

    What is it

    It is derived from both  istream  and   ostream, acts only as a base

    class from which other classes, specifically  iostream withassign,can be derived. There are three withassign classes:

      istream withassign, derived from   istream

      ostream withassign, derived from   ostream

      iostream withassign, derived from  iostream

    Paris Saclay Minh Quang NGUYEN 20

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    21/25

    Disk File I/O with Streams

    Classes

    How to save data to disk files and read it back in? Working withdisk files requires a set of classes

      ifstream  for input

      ofstream  for output

      fstream  for both input and output

    Objects of these classes can be associated with disk files, and wecan use their member functions to read and write to the files.

    Paris Saclay Minh Quang NGUYEN 21

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    22/25

    Disk File I/O with Streams

    Formatted File I/O

    In formatted I/O, numbers are stored on disk as a series of characters.Example: 6.02, rather than being stored as a 4-byte type float or an8-byte type double, is stored as the characters ‘6’, ‘.’, ‘0’, and ‘2’

    Paris Saclay Minh Quang NGUYEN 22

    Chapter 9. STREAMS AND FILES

  • 8/17/2019 Chapter 9. File and Stream

    23/25

    Disk File I/O with Streams

    Writing Data

    Writes a character, an integer, a type double, and two stringobjects to a disk file.

    char ch = ‘x’;

    int j = 77;

    double d = 6.02;string str1 = \Kafka"; //strings without

    string str2 = \Proust"; // embedded spaces

    ofstream outfile(\fdata.txt"); //create ofstream object

    outfile

  • 8/17/2019 Chapter 9. File and Stream

    24/25

    You do it

    Paris Saclay Minh Quang NGUYEN 24

    Chapter 9. STREAMS AND FILES

    Q & A

  • 8/17/2019 Chapter 9. File and Stream

    25/25

    Q & A

    QUESTION and ANSWER

    Paris Saclay Minh Quang NGUYEN 25