03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

46 CHAPTER 2. <strong>C++</strong> BASICS<br />

The first statement declares a variable of type int called age, and the second one waits <strong>for</strong> an<br />

input from cin (the keyboard) in order to store it in this integer variable. The input from the<br />

keyboard is processed once the RETURN key has been pressed.<br />

You can also use cin to request more than one datum input from the user:<br />

std::cin ≫a ≫b;<br />

is equivalent to:<br />

std::cin ≫a;<br />

std::cin ≫b;<br />

In both cases the user must give two data, one <strong>for</strong> variable a and another one <strong>for</strong> variable b that<br />

may be separated by any valid blank separator: a space, a tab character or a newline.<br />

2.7.3 Input/Output with files<br />

C ++ provides the following classes to per<strong>for</strong>m output and input of characters to/from files:<br />

• std::ofstream: used to write to files<br />

• std::ifstream: used to read from files<br />

• std::fstream: used to both read and write from/to files.<br />

We can use file streams the same way we are already used cin and cout, with the only difference<br />

that we have to associate these streams with physical files. Here is an example:<br />

#include <br />

#include <br />

int main () {<br />

std::ofstream myfile;<br />

myfile.open (”example.txt”);<br />

myfile ≪ ”Writing this to a file. ” ≪ std::endl;<br />

myfile.close();<br />

return 0;<br />

}<br />

This code creates a file called example.txt (or overwrites it if it already exists) and inserts a<br />

sentence into it in a way that is similar to the use of cout. C ++ has the concept of an output<br />

streams that is satisfied by an output file as well as be std::cout. That means that everything<br />

that can be written to std::cout can also be written to a file, and vice versa. If you define yourself<br />

the operator ≪ <strong>for</strong> a new type you do not need to program it <strong>for</strong> different output type but<br />

only once <strong>for</strong> a general output stream, see 18<br />

Alternatively, one can give the file stream object the file name as argument. This opens the file<br />

implicitly. The file is also implicitly closed when myfile at some point, in this case at the end<br />

of the main function. The mechanisms that control such implicit actions will become clear in<br />

§ 2.2.3. The bottom line is that you only in few cases must close your files explicitly. The short<br />

version of the previous listing is<br />

18 TODO: Where? New section needed.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!