Title: Chapter 6 IO Streams
1Chapter 6I/O Streams
Goals
- To determine how to use external files for input
and output
- To investigate means by which file output can be
formatted
- To examine how to use I/O streams as function
arguments
- To introduce the concept of end-of-file for
external input
- To explore the nuances of character I/O with get
put
2This library enables the use of external files.
include ltiostreamgt include ltfstreamgt using
namespace std bool userWantsGrosses() void
outputTotalGross(double gross) void main()
int dayCount 0 double dailyGross double
totalGross 0.0 ifstream fileContainingGrosse
s if (userWantsGrosses())
fileContainingGrosses.open("GrossFile.txt")
while (dayCount lt 365)
dayCount fileContainingGrosses gtgt
dailyGross totalGross dailyGross
fileContainingGrosses.close()
outputTotalGross(totalGross) else
cout ltlt "Well, why did you bother " ltlt
"executing this program, " ltlt "you
goof?" ltlt endl ltlt endl return
The declaration of an ifstream variable.
Input Files
Opening the input file in the current directory,
using the filename GrossFile.txt.
When a program needs to retrieve a stream of data
from an external source other than the keyboard,
it may use an input file stream.
Reading the next value from the input file.
Closing the input file. Note that the file name
is not specified for closing.
3bool userWantsGrosses() char yOrN cout
ltlt "Would you like to compute the " ltlt
year\'s grosses? (Enter Y or N) " cin gtgt
yOrN while ((yOrN ! 'y') (yOrN ! 'Y')
(yOrN ! 'n') (yOrN ! 'N'))
cout ltlt "Your response must be the "
ltlt "letter \'Y\' or the letter \'N\'."
ltlt " Please try again " cin gtgt
yOrN return ((yOrN 'y') (yOrN
'Y')) void outputTotalGross(double gross)
cout.setf(iosfixed) cout.setf(iosshowpoin
t) cout.precision(2) cout ltlt "The total
gross is " ltlt gross ltlt endl ltlt endl
804.89 692.45 614.39 760.26 541.53 229.55
954.82 206.73 730.13 685.33 405.49 351.50
152.51 606.81 107.79 127.55 869.49 382.64
605.27 263.36 124.58 544.82 291.35 822.71
077.64 550.15 537.19 363.20 141.80
432.30 843.20 229.25 766.33 964.90 803.19
845.54 819.56 226.56 171.96 726.15 173.52
207.94 306.04 365.12 305.00 652.27 705.38
124.09 394.89 969.18 133.97 582.64 439.77
912.91 547.71 767.07 828.21 165.28 807.91
922.96 885.58 114.42 907.91 747.27 239.85
769.38 991.46 645.89 112.00 601.68 590.91
531.67 283.88 354.00 363.00 129.22 546.80
607.22 950.95 701.57 583.82 445.84 350.40
464.60 788.99 512.66 607.69 704.70 676.68
693.55 928.43 465.61 074.07 222.49 624.66
419.50 719.75 347.79 678.47 078.55 662.02
250.32 350.43 932.13 959.49 753.67 110.79
365.61 737.57 217.85 121.83 926.01 863.44
756.00 149.19 750.56 673.25 166.11 930.27
383.58 614.17 316.75 278.47 177.01 704.67
179.26 150.18 800.91 466.05 645.18 854.05
928.95 128.43 121.07 429.77 114.67 664.27
840.35 574.38 924.22 652.51 383.49 244.99
341.42 126.59 946.36 815.44 510.02 409.20
948.28 315.16 929.74 187.37 619.77 628.32
371.11 230.43 560.67 681.65 324.64 804.34
609.70 307.74 450.59 585.69 515.57 148.97
344.36 927.68 750.45 669.46 783.11 882.45
456.66 788.03 770.01 331.61 766.63 625.02
640.51 229.06 587.56 402.53 347.51 208.60
290.61 738.95 490.06 559.96 395.72 857.07
601.90 725.90 792.64 583.58 465.12 423.84
188.66 768.50 764.49 944.91 423.56 802.31
748.45 133.92 942.87 459.21 912.06 202.64
669.65 244.36 267.78 280.69 665.23 273.30
719.14 955.75 520.38 861.99 904.06 807.55
766.58 354.07 122.16 487.56 648.48 570.39
112.33 648.59 848.26 079.62 996.48 276.74
502.36 169.84 754.52 628.92 800.93 807.36
846.09 629.28 911.40 930.46 345.62 627.60
315.82 948.97 105.19 372.54 503.41 939.60
211.56 903.16 308.51 794.48 552.82 711.78
334.69 111.51 360.07 381.24 855.45 554.16
239.93 649.52 532.68 359.52 763.75 905.22
142.65 821.94 228.62 166.90 651.00 931.04
956.00 348.97 726.92 891.71 644.16 368.09
137.02 604.67 638.97 692.28 609.94 335.46
439.60 586.73 711.97 449.90 404.37 458.75
624.42 244.52 872.43 555.26 763.47 849.55
766.74 316.18 232.93 533.12 994.29 436.36
650.67 884.43 409.04 239.49 639.30 504.23
452.87 517.19 921.75 070.61 840.92 836.06
812.39 878.22 606.18 566.11 644.11 719.23
369.43 733.64 822.44 073.80 349.79 651.63
346.83 808.68 782.12 830.73 789.92 111.56
156.06 724.22 575.78 219.53 642.90 109.59
846.83 329.96 826.12 760.37 561.27 181.54
400.16 111.32 310.52 863.88 554.52 569.27
507.05 643.70 407.44 974.13 569.07 235.70
205.93 161.91 937.90 896.32 856.69 733.53
Contents of GrossFile.txt
Resulting Output
4This library enables the use of external files.
include ltiostreamgt include ltfstreamgt include
ltcstdlibgt include ltctimegt using namespace
std double newGross() void main()
ofstream fileContainingGrosses
fileContainingGrosses.open("GrossFile.txt")
fileContainingGrosses.setf(iosfixed)
fileContainingGrosses.setf(iosshowpoint)
fileContainingGrosses.precision(2) for (int
i 1 i lt 365 i)
fileContainingGrosses ltlt newGross() if (i
10 0) fileContainingGrosses ltlt
endl else fileContainingGrosses
ltlt " " fileContainingGrosses.close()
return
The declaration of an ofstream variable.
Opening the output file in the current directory,
using the filename GrossFile.txt.
Output Files
When a program needs to deliver a stream of data
to an external destination other than the
monitor, it may use an output file stream.
Writing the next value to the output file.
Closing the output file. Note that the file name
is not specified for closing.
5804.89 692.45 614.39 760.26 541.53 229.55
954.82 206.73 730.13 685.33 405.49 351.50
152.51 606.81 107.79 127.55 869.49 382.64
605.27 263.36 124.58 544.82 291.35 822.71
077.64 550.15 537.19 363.20 141.80
432.30 843.20 229.25 766.33 964.90 803.19
845.54 819.56 226.56 171.96 726.15 173.52
207.94 306.04 365.12 305.00 652.27 705.38
124.09 394.89 969.18 133.97 582.64 439.77
912.91 547.71 767.07 828.21 165.28 807.91
922.96 885.58 114.42 907.91 747.27 239.85
769.38 991.46 645.89 112.00 601.68 590.91
531.67 283.88 354.00 363.00 129.22 546.80
607.22 950.95 701.57 583.82 445.84 350.40
464.60 788.99 512.66 607.69 704.70 676.68
693.55 928.43 465.61 074.07 222.49 624.66
419.50 719.75 347.79 678.47 078.55 662.02
250.32 350.43 932.13 959.49 753.67 110.79
365.61 737.57 217.85 121.83 926.01 863.44
756.00 149.19 750.56 673.25 166.11 930.27
383.58 614.17 316.75 278.47 177.01 704.67
179.26 150.18 800.91 466.05 645.18 854.05
928.95 128.43 121.07 429.77 114.67 664.27
840.35 574.38 924.22 652.51 383.49 244.99
341.42 126.59 946.36 815.44 510.02 409.20
948.28 315.16 929.74 187.37 619.77 628.32
371.11 230.43 560.67 681.65 324.64 804.34
609.70 307.74 450.59 585.69 515.57 148.97
344.36 927.68 750.45 669.46 783.11 882.45
456.66 788.03 770.01 331.61 766.63 625.02
640.51 229.06 587.56 402.53 347.51 208.60
290.61 738.95 490.06 559.96 395.72 857.07
601.90 725.90 792.64 583.58 465.12 423.84
188.66 768.50 764.49 944.91 423.56 802.31
748.45 133.92 942.87 459.21 912.06 202.64
669.65 244.36 267.78 280.69 665.23 273.30
719.14 955.75 520.38 861.99 904.06 807.55
766.58 354.07 122.16 487.56 648.48 570.39
112.33 648.59 848.26 079.62 996.48 276.74
502.36 169.84 754.52 628.92 800.93 807.36
846.09 629.28 911.40 930.46 345.62 627.60
315.82 948.97 105.19 372.54 503.41 939.60
211.56 903.16 308.51 794.48 552.82 711.78
334.69 111.51 360.07 381.24 855.45 554.16
239.93 649.52 532.68 359.52 763.75 905.22
142.65 821.94 228.62 166.90 651.00 931.04
956.00 348.97 726.92 891.71 644.16 368.09
137.02 604.67 638.97 692.28 609.94 335.46
439.60 586.73 711.97 449.90 404.37 458.75
624.42 244.52 872.43 555.26 763.47 849.55
766.74 316.18 232.93 533.12 994.29 436.36
650.67 884.43 409.04 239.49 639.30 504.23
452.87 517.19 921.75 070.61 840.92 836.06
812.39 878.22 606.18 566.11 644.11 719.23
369.43 733.64 822.44 073.80 349.79 651.63
346.83 808.68 782.12 830.73 789.92 111.56
156.06 724.22 575.78 219.53 642.90 109.59
846.83 329.96 826.12 760.37 561.27 181.54
400.16 111.32 310.52 863.88 554.52 569.27
507.05 643.70 407.44 974.13 569.07 235.70
205.93 161.91 937.90 896.32 856.69 733.53
Note that this program produces a 365-value
output file that can be used as an input file in
the previous example.
double newGross() static bool firstTime
true long int randomNumberSeed double
lowerBound 100.00 double upperBound
1000.00 if (firstTime)
time(randomNumberSeed) srand(randomNumberS
eed) firstTime false return
(lowerBound (upperBound -
lowerBound) (double(rand()) /
32769))
6Passing Stream Parameters
When passing an input or output stream to a
function, make sure to pass it by reference.
After being opened, the input stream keeps track
of how much of the file has been read, and this
will probably be changed inside the function to
which the stream is passed. (Otherwise, why
bother passing the stream?)
After being opened, the output stream buffers the
data being written to it, and the contents of
this buffer will probably change inside the
called function. (Again, why else would the
stream be passed?)
7include ltiostreamgt include ltfstreamgt include
ltcassertgt using namespace std double
computeMonthlyGross(ifstream inputFile, int
monthNbr) void outputMonthlyGross(ofstream
outputFile, int monthNbr, double gross) void
main() int month char dailyGrossFileName
50 ifstream dailyGrossFile char
monthlyGrossFileName50 ofstream
monthlyGrossFile cout ltlt "Enter the name of
the file containing the daily gross amounts "
cin gtgt dailyGrossFileName dailyGrossFile.open
(dailyGrossFileName) assert(!dailyGrossFile.fa
il()) cout ltlt endl ltlt endl cout ltlt
"Enter the name of the file that will contain the
monthly gross amounts " cin gtgt
monthlyGrossFileName monthlyGrossFile.open(mon
thlyGrossFileName) assert(!monthlyGrossFile.fa
il()) cout ltlt endl ltlt endl
monthlyGrossFile.setf(iosfixed)
monthlyGrossFile.setf(iosshowpoint)
monthlyGrossFile.precision(2) for (month
1 month lt 12 month) outputMonthlyGross(
monthlyGrossFile, month, computeMonthlyGross(daily
GrossFile, month)) dailyGrossFile.close()
monthlyGrossFile.close() return
An assert statement is used to terminate the
program if the asserted condition is not true.
8double computeMonthlyGross(ifstream inputFile,
int monthNbr) int monthLength, dayNbr
double dayGross, monthGross 0.0 if
(monthNbr 2) monthLength 28 else
if ((monthNbr 4) (monthNbr 6)
(monthNbr 9) (monthNbr 11))
monthLength 30 else monthLength
31 for (dayNbr 1 dayNbr lt monthLength
dayNbr) inputFile gtgt dayGross
monthGross dayGross return
monthGross void outputMonthlyGross(ofstream
outputFile, int monthNbr, double gross)
static double annualGross 0.00 outputFile
ltlt "MONTH " ltlt monthNbr ltlt endl outputFile
ltlt '' ltlt gross ltlt endl ltlt endl annualGross
gross if (monthNbr 12)
outputFile ltlt "ANNUAL TOTAL" ltlt endl
outputFile ltlt '' ltlt annualGross ltlt endl ltlt
endl return
MONTH 1 15822.43 MONTH 2 14942.34 MONTH
3 17826.55 MONTH 4 17439.44 MONTH
5 15606.27 MONTH 6 17012.34
MONTH 7 16944.92 MONTH 8 19791.42 MONTH
9 15048.91 MONTH 10 18340.46 MONTH
11 20008.17 MONTH 12 16076.68 ANNUAL
TOTAL 204859.93
Final contents of Summary.txt.
Sample interactive session
9 cout ltlt "What temperature is " ltlt
"considered too cold " ltlt "(in degrees)?
" cin gtgt tooCold cout ltlt "What
temperature is " ltlt "considered too hot
" ltlt "(in degrees)? " cin gtgt
tooHot weatherFile.open("Temps.txt")
weatherFile gtgt month while (!weatherFile.eof()
) weatherFile gtgt dummyChar
weatherFile gtgt day weatherFile gtgt
dummyChar weatherFile gtgt year
weatherFile gtgt lowTemp if (lowTemp lt
tooCold) lowCount weatherFile
gtgt highTemp if (highTemp gt tooHot)
highCount weatherFile gtgt month
cout ltlt lowCount ltlt " COLD DAYS" ltlt endl
cout ltlt highCount ltlt " HOT DAYS" ltlt endl
ltlt endl return
Using eof
Whenever an input operation fails to retrieve a
new value, the input files eof is set to true.
The format of an input file must be known in
advance. But the length of the input file doesnt
have to be set in stone!
include ltiostreamgt include ltfstreamgt using
namespace std void main() double tooCold,
tooHot double lowTemp, highTemp int
month, day, year char dummyChar int
lowCount 0, highCount 0 ifstream
weatherFile
101/3/07 -17 2 1/7/07 -10 5 1/11/07
-2 11 1/19/07 8 16 1/27/07 5 18 2/4/07
10 22 2/6/07 11 19 2/10/07 16 28 2/14/07
17 23 2/18/07 21 29 2/24/07 24 31 3/2/07
30 35 3/6/07 31 37 3/11/07 31 40 3/17/07
34 38 3/22/07 32 42 3/27/07 37 45 4/3/07
36 41 4/8/07 39 46 4/12/07 42 50 4/19/07
45 53 4/27/07 48 55
Notice that the program assumed that the format
of each line in the input file was an integer
month, followed by a single character, then an
integer day, followed by another character, then
an integer year, followed by some white space,
and, finally, an integer low temperature and an
integer high temperature.
5/1/07 53 59 5/7/07 57 62 5/12/07
58 67 5/19/07 60 70 5/27/07 64 71 5/31/07
73 79 6/3/07 75 83 6/7/07 77 84 6/13/07
81 89 6/17/07 84 88 6/26/07 92 94 6/30/07
91 95 7/2/07 94 99 7/8/07 90 95 7/11/07
92 98 7/17/07 90 96 7/28/07 95 98 8/3/07
92 96 8/8/07 88 95 8/12/07 87 95 8/16/07
86 90 8/23/07 85 91 8/29/07 84 88
9/2/07 80 86 9/5/07 81 84 9/12/07
75 79 9/18/07 74 77 9/23/07 73 78 9/29/07
70 78 10/4/07 68 75 10/8/07 65 69 10/15/07
66 70 10/21/07 57 63 10/26/07 54 59 11/1/07
52 63 11/6/07 50 56 11/13/07 46 53 11/18/07
44 52 11/25/07 38 49 11/30/07 36 42 12/3/07
33 34 12/7/07 26 31 12/13/07 19 23 12/17/07
16 20 12/22/7 11 18 12/28/8 5 10
Contents of Temps.txt
Resulting Output
11include ltiostreamgt include ltfstreamgt using
namespace std const int MAX_FILENAME_LENGTH
50 bool isWhiteSpace(char ch) void main()
ifstream originalFile ofstream
revisedFile char originalFileNameMAX_FILENAM
E_LENGTH char revisedFileNameMAX_FILENAME_LE
NGTH char currentCharacter bool
justHadWhiteSpace false cout ltlt "Enter the
name of the file " ltlt "containing the
original text " cin gtgt originalFileName
cout ltlt "Enter the name of the where " ltlt
" the revised text will be stored " cin gtgt
revisedFileName originalFile.open(originalFil
eName) revisedFile.open(revisedFileName)
Character I/O
Input files have a built-in member function
called get to facilitate reading
characters. Similarly, output files have a
built-in member function called put to
facilitate writing characters.
12 originalFile.get(currentCharacter) while
(!originalFile.eof()) if
(isWhiteSpace(currentCharacter))
if (!justHadWhiteSpace)
revisedFile ltlt endl
justHadWhiteSpace true
else justHadWhiteSpace
false revisedFile.put(currentCharacter)
originalFile.get(currentCharacter)
originalFile.close()
revisedFile.close() return bool
isWhiteSpace(char ch) return ((ch ' ')
(ch '\a') (ch '\b')
(ch '\n') (ch '\r') (ch '\t'))
include ltiostreamgt include ltfstreamgt using names
pace std const int MAX_FILENAME_LENGTH 50 bool
isWhiteSpace(char ch) void main() ifstream ori
ginalFile ofstream revisedFile char originalFile
NameMAX_FILENAME_LENGTH char ?
Input one character
Output one character
Beginning of the output file Lexical.txt
Sample interactive session