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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Basic File Input<br />

Read<strong>in</strong>g a text file <strong>in</strong> <strong>C++</strong> is a fairly pa<strong>in</strong>less task. List<strong>in</strong>g 4.4 conta<strong>in</strong>s a program that reads<br />

its own source file and displays each l<strong>in</strong>e as it reads it from disk. First, enter the program as<br />

it appears <strong>in</strong> the list<strong>in</strong>g (remember, don’t type the l<strong>in</strong>e numbers). Then save the project with<br />

the name READFILE. If you don’t save the program with this name, the program will not run<br />

properly. Compile and run the program. S<strong>in</strong>ce the program reads its own source file, the<br />

output from the program will be the contents of List<strong>in</strong>g 4.4.<br />

List<strong>in</strong>g 4.4. READFILE.CPP.<br />

1: #<strong>in</strong>clude <br />

2: #<strong>in</strong>clude <br />

3: #<strong>in</strong>clude <br />

4: #<strong>in</strong>clude <br />

5: #<strong>in</strong>clude <br />

6: #<strong>in</strong>clude <br />

7: #pragma hdrstop<br />

8:<br />

9: <strong>in</strong>t ma<strong>in</strong>(<strong>in</strong>t argc, char **argv)<br />

10: {<br />

11: char buff[80];<br />

12: ifstream <strong>in</strong>file;<br />

13: <strong>in</strong>file.open(“readfile.cpp”);<br />

<strong>14</strong>: if (!<strong>in</strong>file) return 0;<br />

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

16: <strong>in</strong>file.getl<strong>in</strong>e(buff, sizeof(buff));<br />

17: cout

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

Saved successfully!

Ooh no, something went wrong!