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 />

For example, sometimes you want to append data to the end of an exist<strong>in</strong>g file rather than<br />

create a new file. In that case you can append data to the end of a file by open<strong>in</strong>g the file <strong>in</strong><br />

append mode. To specify append mode, you must use one of the ios class’s open_mode<br />

specifiers <strong>in</strong> the ofstream constructor when you create the object:<br />

ofstream outfile(“test.dat”, ios::app); // open <strong>in</strong> append mode<br />

This file will be opened <strong>in</strong> append mode and any new data written to the file will be written<br />

to the end of the file. There are several specifiers you can use when open<strong>in</strong>g files. Table 4.1<br />

lists the open_mode enumeration’s values and their descriptions.<br />

Table 4.1. ios class open_mode specifiers.<br />

Specifier Description<br />

app The file is opened and any new data will be appended to the end of<br />

the file.<br />

ate Seek to the end of the file when the file is opened.<br />

<strong>in</strong> The file is opened for <strong>in</strong>put (read<strong>in</strong>g). This is the default for the<br />

ifstream class.<br />

out The file is opened for output (writ<strong>in</strong>g). This is the default for the<br />

ofstream class.<br />

b<strong>in</strong>ary The file is opened <strong>in</strong> b<strong>in</strong>ary mode. The default is to open the file <strong>in</strong><br />

text mode. In text mode, when the file is read, carriage-return/<br />

l<strong>in</strong>efeed (CR/LF) pairs are converted to a s<strong>in</strong>gle l<strong>in</strong>efeed character<br />

(LF). When the file is written, l<strong>in</strong>efeed characters are converted to<br />

CR/LF pairs before be<strong>in</strong>g written to the file. In b<strong>in</strong>ary mode no<br />

conversion of CR/LR pairs takes place.<br />

trunc Opens the file and clears the contents. If neither app nor ate are<br />

specified, trunc is the default.<br />

nocreate The file will not be created if it does not exist. Open() will fail if the<br />

file does not exist. Open<strong>in</strong>g an ifstream with this flag can be used<br />

to check for the existence of a file.<br />

noreplace Similar to nocreate except that Open will fail unless either app or ate<br />

is specified.<br />

You can or together two or more of the values listed <strong>in</strong> Table 4.1 if needed. For example, let’s<br />

say you wanted to open a file <strong>in</strong> b<strong>in</strong>ary mode and that you wanted to append data to the end<br />

of the file. In that case the constructor would look like this:<br />

ofstream outfile(“test.dat”, ios::app | ios::b<strong>in</strong>ary);<br />

127<br />

4

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

Saved successfully!

Ooh no, something went wrong!