Changes Made from the Second Printing to the Third

Chapter 8

Page

Line

Change

To

465

10-11, 13-14 of Fig.8.1

iostream.h
fstream.h
assert.h
float.h
Insert after line 14:

iostream
fstream
cassert
cfloat
using namespace std;

468

Footnote

object cerr is

screen.

objects cerr and cerr are

screen (see Section 4.3).

469

2nd sentence of 1st para

Replace

An fstream is used both for input from and output to a file.

 

5T

fstream.h

fstream

 

6T

Declaring an Fstream Object

Declaring a File Stream

 

17-18

Replace these 2 sentences: "Once an . . . this section."

Using fstream instead of ifstream or ofstream constructs an fstream object that can be used both for input from and output to a file.

 

20-25

Replace first two sentences of paragraph.

It is important to understand that the classes ifstream, ofstream, and fstream are derived from the classes istream, ostream, and iostream, respectively, which means that these classes have been constructed as extensions of the istream, ostream, and iostream classes. As a result, all of the operations on istream, ostream, and iostream objects can also be performed on ifstream, ofstream, and fstream objects, respectively, along with several new operations that are limited to file streams.

 

Footnote

Replace

2 As noted before, older C++ compilers may require <fstream.h> in place of <fstream> and removal of the using namespace std; line.

470

5-7B

Replace

For a file name stored in a string object, the function member data() or c_str() must be used to extract the character string literal from the string. (c_str() appends a terminating null character – see Section 9.2 – whereas data() does not.)

471

5B

fstream

filestream

472

6T

Delete reference to footnote 5

 

 

17-18T

Delete the entries ios::nocreate and ios::noreplace in the table

 

 

25T

Add the following at the end of this paragraph.

The file modes can also be combined using the bitwise-or (|) operator. For example,

fstream inoutStream;
inoutStream.open("TwoWayFile",
ios::in | ios::out);

opens the file TwoWayFile for both input and output.

 

6B

fstream

ofstream

 

Footnote

Delete the footnote

 

473

1T

an fstream

a file stream

 

5T

Delete "or"

 

 

6T

Insert a new 3rd line in boxed display

fstream fstream_name(file_name, mode);

 

8T

fstream being initialized

file stream being initialized

 

10T

described above

described earlier

 

5-6B

an fstream object

a file stream

474

2T

an fstream to a file

a stream to a file

 

11T

footnote number 4

3

 

16T

name of an fstream

name of a file stream

 

10B

test each fstream

test each file stream

 

Footnote

ANSI-C++

ISO/ANSI-C++

475

1T

when an fstream

when a stream

 

9T

opening an ifstream

opening a stream

 

1-3 of Fig. 8.3

iostream.h
fstream.h
Insert after line 3:

iostream
fstream
using namespace std;

482

14B

of an fstream

of a stream

484

1T

of an fstream

of a file stream

 

3T

an fstream is

a file stream is

 

6-7

disconnect an fstream

disconnect a file stream

 

18T

name of an fstream

name of a stream

 

19T, 21T

Delete "output"

 

485

1T

Footnote number 5

4

 

7T

Fstreams as Parameters

File Streams as Parameters

 

8T

Fstreams,

File streams,

 

9T

fstream arguments

file stream arguments

 

14T

fstream parameter

file stream parameter

 

22T

through an fstream

through a file stream

 

15B

connect an fstream

connect a file stream

 

14B

then the fstream

then the file stream

 

13B

in the fstream

in the file stream

 

12B

then the fstream

then the stream

 

8B

and the fstream

and the stream

 

Footnote

5

4

486

8T

an fstream

a stream

 

19T — QQ#5

The types of fstreams

The types of streams

 

22T — QQ#6

performed on fstreams

performed on file streams

 

23T — QQ#7

declare an fstream

declare a file stream

 

27T — QQ#9

for an fstream

for a file stream

487

3T & 5T

open an fstream

open a file stream

 

12T

is an istream

is an ifstream

489

24T

Footnote number 6

5

 

7-9 of Fig. 8.4

iostream.h
fstream.h
Insert after line 9:

iostream
fstream
using namespace std;

 

Footnote

6

5

490

16T

NPOS

string::npos

491-503

Right running heads at top of page

ADDITIONAL FSTREAM OPERATIONS

ADDITIONAL STREAM FEATURES

491

1T

Additional Fstream Operations

Additional Stream Features

 

4T

fstreams.

file streams.

 

11T & 15T

most of the operations

the operations

 

4B

additition

addition

492

7T-18T

Delete these lines

 

 

23T

offset from the beginning of

position in

 

Last paragraph

Replace

This change along with others on pp. 492-504 results in new layouts of pages – e.g., material getting moved forward one page – so that new material on string streams can be added on pp. 503-504.

Two passes must be made through the file to solve this problem. The first pass calculates the sum and a count of the numbers. Their average can then be calculated:

  if (count > 0) average = sum / count;

A second pass must then be made to calculate and display the difference between each number and this average.

493

9-11 of Fig. 8.5

iostream.h
fstream.h
assert.h
Insert after line 11:

iostream
fstream
assert
using namespace std;

494

11T

Footnote number 7

6

 

12-17T

Replace

ios::beg: move read position offset bytes from beginning of the stream

ios::cur: move read position offset bytes from its current position

ios::end: move read position offset bytes from end of the stream

 

1-4B

Replace

Then the following call can be used to move the read position 2 bytes before the end-of-file mark in inStream:

  inStream.seekg(-2, ios::end);

 

Footnote

7

6

495

1-3T

Replace

We can reset the read position to the first character in inStream with

  instream .seekg(0, ios::beg);

 

4-6T

Replace

advance the read position by three bytes from its current position with

  instream .seekg(3, ios::cur);

 

7-9T

Replace

and move the read position backward one byte from its current position with

  instream .seekg(-1, ios::cur);

498

7-11T

Replace

can be used to to compute the number of characters
(
offBy = 3) that the read position is past the beginning of the current line; and the following statement can then be used to move the read position forward to the beginning of the next line:

  friendStream.seekg(LINE_LENGTH - offBy, 
ios::cur); // go to beginning of next line

 

14T

Footnote number 8

7

 

Footnote

8

7

499

13T

Footnote number 9

8

 

Footnote

9

ctype

8

cctype

502

16T

Delete footnote reference 10

 

 

Footnote

Delete

 

504

9B

Insert the following new subsection

 

 

String Streams

A recent feature added to C++ is the ability to read input from a string or to write output to a string. This is made possible by means of string streams defined in the <sstream> library:

istringstream For input from a string

ostringstream For output to a string

stringstream For input from and output to a string

(The corresponding types for wide characters (of type wchar_t) are wistringstream, wostringstream, and wstringstream.) The member function str() can be used to convert a string to a string stream and vice versa:

strstream.str(s); Set string stream strstream to a copy of string s

str(strstream) Returns a string that is a copy of the string in strstream

The program in Figure 8.7 illustrates the use of string streams. It constructs an istringstream from the string date, uses the input operator >> to read the individual words and integers, and displays them. It then outputs strings and integers to the ostringstream ostr, uses str() to copy the string in ostr, and displays it.

 

Figure 8.7 String Streams.


/* stringstreams7.cpp illustrates the use of string streams. 
*  Input (istringstream istr):  word1, word2, month, day, comma, year
*  Output (istringstream ostr): these items separated by ***
*  Output (ostream cout):       the string stored in ostr
*********************************************************************/
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

int main()
{
  string date = "Independence Day: July 4, 1776";
  istringstream istr(date);
  string word1, word2, month;
  int day, year, one = 1;
  char comma;

  istr >> word1 >> word2 >> month >> day >> comma >> year;
  cout << "Contents of string stream istr:\n"
       << word1 << "***" << word2 << "***"<< month << "***"
       << day << "***"<< comma << "***" << year << endl;
  ostringstream ostr; 
  ostr << "New Year's " << word2 << " January"
       << setw(2) << one << ", " << year + 224;
  cout << ostr.str() << endl;
} 

Sample Run:

Contents of string stream istr:
Independence***Day:***July***4***,***1776
New Year's Day: January 1, 2000

 

505

7T—QQ#8

in the fstream

in the file stream

 

9T—QQ#9

in the fstream

in the file stream

 

10T—QQ#9

from the fstream

from the file stream

512

8T

Before an fstream

Before a file stream

 

9T

the fstream

the file stream

 

21-22T

the string function member data() must be used

the function member data() or c_str() must be used

 

24T

on an fstream

on a file stream

514

15-16T

fstream

file stream

 

 

 

 

 

Changes in Chapter 9