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.

122 Day 4<br />

NOTE<br />

Basic File I/O<br />

Classes <strong>in</strong> VCL do not support multiple <strong>in</strong>heritance. You can still use<br />

multiple <strong>in</strong>heritance <strong>in</strong> any classes you write for use <strong>in</strong> your <strong>C++</strong>Builder<br />

applications.<br />

It won’t be long before you are go<strong>in</strong>g to need the ability to read and write files <strong>in</strong> your<br />

applications. I saved this discussion for now because you needed an understand<strong>in</strong>g of classes<br />

<strong>in</strong> order to understand how file <strong>in</strong>put and output is handled <strong>in</strong> <strong>C++</strong>.<br />

If you are go<strong>in</strong>g to be do<strong>in</strong>g heavy database operations with <strong>C++</strong>Builder, you might be<br />

relieved to know that you don’t have to worry about deal<strong>in</strong>g with read<strong>in</strong>g and writ<strong>in</strong>g<br />

database files directly. That is all handled for you beh<strong>in</strong>d the scenes through <strong>C++</strong>Builder’s<br />

VCL database components. VCL also provides support for read<strong>in</strong>g and writ<strong>in</strong>g the contents<br />

of edit controls, list boxes, and other basic W<strong>in</strong>dows controls. The W<strong>in</strong>dows API provides<br />

functions for read<strong>in</strong>g configuration files (.INI files). Many OCX and ActiveX controls know<br />

how to save and load files specific to the type of action the control performs.<br />

As you can see, many of the objects you will use <strong>in</strong> <strong>C++</strong>Builder will handle file I/O for you.<br />

Still, there will be times when you have to read and write your own files, and you’ll need a<br />

basic understand<strong>in</strong>g of file I/O <strong>in</strong> order to do that.<br />

Basic file I/O is accomplished via three <strong>C++</strong> classes:<br />

■ The ofstream class, which handles file output<br />

■ The ifstream class, which handles file <strong>in</strong>put<br />

■ The fstream class, which handles both file <strong>in</strong>put and output<br />

These classes are derived from the iostream class. You have had some experience with<br />

iostream already, although you may not know it. The cout and c<strong>in</strong> classes I have been us<strong>in</strong>g<br />

for console <strong>in</strong>put and output are also derived from iostream. iostream itself is derived from<br />

a class called ios, which is the base class for all stream <strong>in</strong>put and output <strong>in</strong> <strong>C++</strong>.<br />

Does this sound complicated? Well, it is. Sometimes the labyr<strong>in</strong>th of the stream<strong>in</strong>g classes<br />

can be pretty confus<strong>in</strong>g. What I am go<strong>in</strong>g to do here is give you a cursory glance at file I/O,<br />

but then we’ll have to move on. You’ll be relieved to know that basic file I/O is not terribly<br />

complicated. If you need to do sophisticated file operations, you’ll need to dig <strong>in</strong>to the class<br />

library help files or get a good book on <strong>C++</strong> (such as <strong>Teach</strong> <strong>Yourself</strong> <strong>Borland</strong> <strong>C++</strong> 4.5 <strong>in</strong> 21<br />

<strong>Days</strong>, Second Edition by Sams Publish<strong>in</strong>g) that deals with file I/O <strong>in</strong> detail.

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

Saved successfully!

Ooh no, something went wrong!