Input and Output

To be useful, a program must communicate with the world and save calculated results on disk. Most C++ systems do not automatically show the result of every expression the way UnderC does. C++ programs use the iostreams library, which defines input and output “streams”.

Writing to cout

With C++, a program can direct values and text to output by using the insertion operator (<<), acting on cout (pronounced “see-out”), which is a special variable that represents the standard output stream. (Bjarne Stroustrup suggests we call << “put to.”) The following example uses << to output a string literal and an integer. Note that the items follow each other on output with no extra spaces:

;> cout << "nn = " << nn << endl;
nn = 2
(ostream&) ostream { }

Although this is an expression that has a displayed value, we are not interested in the result. The statement is executed for the effect of the insertions, which is to output a text value, an integer value, and endl, which forces a new line. The following example shows the effect of endl, and note that in this example, the last integer has not been left on its own line:

;> cout << 1 << endl << 2 << endl << 3;
1
2
3(ostream&) ostream { }

Evaluating an expression because of its effect on the environment or some object is common in programming. We are not always interested in its value.

The operator << has a lower precedence than any other mathematical operator, so you can safely use expressions with it, but comparisons are a problem. You usually get some sort of error message when you use it, so you will know when you have a problem. The first statement executes fine, because of <<'s low precedence, but the second causes an error:


;> cout << "the answer is " << 20*10+1 << endl;
the answer is 201
;> cout << "a < b = " << a < b << endl;
CON 36:Could not match void operator<<(int,_Endl_);

Because < has lower precedence than <<, the system has to make sense of (b << endl), and it fails. This is another instance where you might want to use extra parentheses to clarify the precedence order you desire.

Reading from cin

You can use the extraction operator (>>), called “get from,” to retrieve values from the standard input stream, cin (pronounced “see-in”). This code prompts the user for an integer; in the case of the example that follows, 42 is then typed in:


;> cin >> nn;
42
(istream&) istream { }
 ;> nn;
(int) nn = 42
;> int i1,i2;
;> string s1,s2;
;> cin >> i1 >> i2 >> s1 >> s2;
10 20
30 dog
(istream&) istream { }

You can extract multiple values from the input stream, ignoring whitespace in the input. Note that strings are assumed to be separated by spaces; the input is assumed to form a continuous stream of characters, not be broken by lines.

Other languages, such as BASIC, have line-oriented input (that is, each input statement grabs a whole line), which makes it quite difficult to perform stunts like the previous example. However, sometimes in C++ you need to read in a whole line at a time. You can do this using the getline() function, which works with an input stream such as cin and reads in the line as a string:

;> getline(cin,s1);
a sentence is composed of words
;> s1;
(string) 'a sentence is composed of words'

Writing and Reading Files

Writing a file involves creating an output stream and using the insertion operator, exactly as for standard output. First you declare a variable of type ofstream, and then you use it to open the file. Then the file is written to in precisely the same way as writing to cout, and when you are finished writing to the file, you close it:


;> ofstream out;
;> out.open("tmp.txt");
(bool) true
;> out << i1 << " " << i2 << endl;
(ostream&) ostream { }
;> out.close();
						

You view the new file by using Notepad, or you can directly execute the DOS type command from the ;> prompt:

;> #x type tmp.txt
10 20

To read a file, you need to use ifstream, which is a file input stream. Again, the sequence of operations is the same; you open the stream, giving it the name of an existing file, and then use >> to extract values from the stream. When finished, it's important to close the file.


>ifstream in;
;> in.open("tmp.txt");
(bool) true;
;> in >> i1 >> i2;
;> in.close();
						

Reading from Strings

It is often useful to extract numbers from a string. The easiest way to do this is by using a special kind of input stream, called istringstream:


;> p
							=
							"10 20 fred";
;> istringstream is(p);
;> is >> i1 >> i2 >> s;
(istream&) istream { }
;> cout << i1 << "," << i2 << s << endl;
10,20fred
(ostream&) ostream { }

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.123