1. The streams fin
and fout
are declared as follows:
ifstream fin;
ofstream fout;
The include
directive that goes at the top of your file is
#include <fstream>
Your code also needs the following:
using namespace
std;
fin.open("stuff1.dat");
if
(fin.fail( )) { cout << "Input file opening failed.\n"; exit(1); } fout.open("stuff2.dat");
if
(fout.fail( )) { cout << "Output file opening failed.\n"; exit(1); }
fin.close( );
fout.close( );
4. You need to replace the stream outStream
with the stream cout
. Note that you do not need to declare cout
, you do not need to call open
with cout
, and you do not need to close cout
.
#include <cstdlib>
Your code also needs the following:
using namespace
std;
6. The exit(1)
function returns the argument to the operating system. By convention, the operating system uses a 1
as an indication of error status and 0
as an indication of success. What is actually done is system-dependent.
7. bla.dobedo(7);
8. Both files and program variables store values and can have values retrieved from them. Program variables exist only while the program runs, whereas files may exist before a program is run and may continue to exist after a program stops. In short, files may be permanent; variables are not. Files provide the ability to store large quantities of data, whereas program variables do not provide quite so large a store.
9. We have seen the open
, close
, and fail
member functions at this point. The following illustrate their use.
intc; ifstream in; ofstream out; in.open("in.dat");
if
(in.fail( )) { cout << "Input file opening failed.\n"; exit(1); } in >> c; out.open("out.dat");
if
(out.fail( )) { cout << "Output file opening failed.\n"; exit(1); } out << c; out.close( ); in.close( );
10. This is the “starting over” the text describes at the beginning of this chapter. The file must be closed and opened again. This action puts the read position at the start of the file, ready to be read again.
11. The two names are the external file name and the stream name. The external file name is the one used by the operating system. It is the real name of the file, but it is used only in the call to the function open
, which connects the file to a stream. The stream name is a stream variable (typically of type ifstream
or ofstream
). After the call to open
, your program always uses the stream name as the name of the file.
* 123*123*
* 123*123*
Each of the spaces contains exactly two blank characters. Notice that a call to width
or call to setw
only lasts for one output item.
13. * 123*123 * 123*
Each of the spaces consists of exactly two blank characters.
* 123*123*
* +123*+123*
*123 *123 *
There is just one space between the *
and the +
on the second line. Each of the other spaces contains exactly two blank characters.
15. The output to the file stuff.dat
will be exactly the same as the output given in the answer to Exercise 14.
16. *12345*
Notice that the entire integer is output even though this requires more space than was specified by setw
.
ios::fixed
. Setting this flag causes floating-point numbers not to be displayed in e
-notation, that is, not in scientific notation. Setting this flag unsets ios::scientific
.
ios::scientific
. Setting this flag causes floating-point numbers to be displayed in e
-notation, that is, in scientific notation. Setting this flag unsets ios::fixed
.
ios::showpoint
. Setting this flag causes the decimal point and trailing zeros to be always displayed.
ios::showpos
. Setting this flag causes a plus sign to be output before positive integer values.
ios::right
. Setting this flag causes subsequent output to be placed at the right end of any field that is set with the width
member function. That is, any extra blanks are put before the output. Setting this flag unsets ios::left
.
ios::left
. Setting this flag causes subsequent output to be placed at the left end of any field that is set with the width
member function. That is, any extra blanks are put after the output. Setting this flag unsets ios::right
.
18. You need to replace outstream
with cout
and delete the open
and close
calls for outstream
. You do not need to declare cout
, open cout
, or close cout
. The #include <fstream>
directive has all the iostream
members you need for screen I/O, though it does no harm, and may make the program clearer, to #include <iostream>
.
1
2
3
3
void
toScreen(ifstream& fileStream) {
int next;
while
(fileStream >> next) cout << next << endl; }
21. The maximum number of characters that can be typed in for a string variable is one less than the declared size. Here the value is 20.
22. The statement
cin >> c;
reads the next nonwhite character, whereas
cin.get(c);
reads the next character whether the character is nonwhite or not.
23. The two statements are equivalent. Both of the statements output the value of the variable c
.
24. The character that is “put back” into the input stream with the member function putback
need not be the last character read. If your program reads an 'a'
from the input stream, it can use the putback
function to put back a 'b'
. (The text in the input file will not be changed by putback
, although your program will behave as if the text in the input file had been changed.)
25. The complete dialogue is
Enter a line of input:
a b c d e f ga b END OF OUTPUT
26. The complete dialogue is
Enter a line of input:
abcdef ghace h
Note that the output is simply every other character of the input, and note that the blank is treated just like any other character.
27. The complete dialogue is
Enter a line of input:
0 1 2 3 4 5 6 7 8 9 10 11
01234567891 1
Be sure to note that only the '1'
in the input string 10
is output. This is because cin.get
is reading characters, not numbers, and so it reads the input 10
as the two characters, '1'
and '0'
. Since this code is written to echo only every other character, the '0'
is not output. Since the '0'
is not output, the next character, which is a blank, is output, and so there is one blank in the output. Similarly, only one of the two '1'
characters in 11
is output. If this is unclear, write the input on a sheet of paper and use a small square for the blank character. Then, cross out every other character; the output shown above is what is left.
28. This code contains an infinite loop and will continue as long as the user continues to give it input. The Boolean expression (next != '\n')
is always true because next
is filled via the statement
cin >> next;
and this statement always skips the new-line character '\n'
(as well as any blanks). The code will run and if the user gives no additional input, the dialogue will be as follows:
Enter a line of input:
0 1 2 3 4 5 6 7 8 9 10 11
0246811
Notice that the code in Self-Test Exercise 27 used cin.get
, so it reads every character, whether the character is a blank or not, and then it outputs every other character. So the code in Self-Test Exercise 27 outputs every other character even if the character is a blank. On the other hand, the code in this Self-Test Exercise uses cin
and >>
, so it skips over all blanks and considers only nonblank characters (which in this case are the digits '0'
through '9'
). Thus, this code outputs every other nonblank character. The two '1'
characters in the output are the first character in the input 10
and the first character in the input 11
.
void
copyChar(istream& sourceFile) {
char
next; sourceFile.get(next); cout << next; }
void
copyLine(istream& sourceFile) {
char
next;
do
{ sourceFile.get(next); cout << next; }
while
(next != '\n'); }
void
sendLine(ostream& targetStream) {
char
next;
do
{ cin.get(next); targetStream << next; }
while
(next != '\n'); }
2.0 1.1 2.3
2.0 3.0 2.3
2.0 3.0 4.0
33. One set of functions follows:
void
func(double
x) {
double
y = 1.1;
double
z = 2.3; cout << x << " " << y << " " << z << endl; }
void
func(double
x,
double
y) {
double
z = 2.3; cout << x << " " << y << " " << z << endl; }
void
func(double
x,
double
y,
double
z) { cout << x << " " << y << " " << z << endl; }
34. It would evaluate to false. Your program must attempt to read one more character (beyond the last character) before it changes to true.
void
textToScreen(ifstream& fileStream) {
char
next; fileStream.get(next);
while
(! fileStream.eof( )) { cout << next; fileStream.get(next); } }
If you prefer, you can use cout.put(next);
instead of cout << next;
.
36. The complete dialogue is as follows:
Enter a line of input:
I'll see you at 10:30 AM.
I'll see you at 1 <END OF OUTPUT
cout << "Enter a line of input:\n";
char next;
do
{
cin.get(next);
if (!isupper(next))
cout << next;
} while (next != '\n');
Note that you should use !isupper(next)
and not use islower(next)
. This is because islower(next)
is false if next
contains a character that is not a letter (such as the blank or comma symbol).