12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

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.

Totally Immersed: <strong>C++</strong> Classes and Object-Oriented Programm<strong>in</strong>g<br />

operator (>>) stops at the first whitespace it encounters. (Whitespace <strong>in</strong>cludes blank spaces,<br />

tabs, and so on.) The getl<strong>in</strong>e() function, on the other hand, reads from the file until an EOL<br />

(end-of-l<strong>in</strong>e) character is detected, which is what you want when read<strong>in</strong>g l<strong>in</strong>es of text. When<br />

read<strong>in</strong>g s<strong>in</strong>gle values without whitespace, the extraction operator is very useful. The follow<strong>in</strong>g<br />

code snippet reads a file conta<strong>in</strong><strong>in</strong>g numbers and outputs the numbers to the screen:<br />

ifstream <strong>in</strong>file(“somefil.dat”);<br />

while (!<strong>in</strong>file.eof()) {<br />

<strong>in</strong>t x;<br />

<strong>in</strong>file >> x; // read number from file and assign it to x<br />

cout

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

Saved successfully!

Ooh no, something went wrong!