25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

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

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

240 ” Streams and Network Programming<br />

line using fgets(), which will continue to fetch data until it reaches a newline character.<br />

Finally, we truncate the file using ftruncate(), increment the counter and write<br />

the new counter value to the file using fwrite().<br />

One thing to take notice of is the second argument to fopen(); this determines two<br />

things: first, whether we are reading, writing or doing both things to the file at the<br />

same time. Secondly, if the file pointer—the position at which the next byte will be<br />

read or written—is set at the beginning or at the end of the file. This flag can take on<br />

one of these values:<br />

Mode Result<br />

r Opens the file for reading only and places the file pointer at the<br />

beginning of the file<br />

r+ Opens the file for reading and writing; places the file pointer at<br />

the beginning of the file<br />

w Opens the file for writing only; places the file pointer at the<br />

beginning of the file and truncate it to zero length<br />

w+ Opens the file for writing and reading; places the file pointer at<br />

the beginning of the file and truncate it to zero length<br />

a Opens the file for writing only; places the file pointer at the end<br />

of the file<br />

a+ Opens the file for reading and writing; places the file pointer at<br />

the end of the file<br />

x Creates a new file for writing only<br />

x+ Creates a new file for reading and writing<br />

Each of these modes can be coupled with a modifier that indicates how the data is<br />

to be read and written: the b flag (e.g.: w+b) forces “binary” mode, which will make<br />

sure that all data is written to the file unaltered. There is also a Windows only flag, t,<br />

which will transparently translate UNIX newlines (\n) to Windows newlines (\r\n).<br />

In addition, the w, w+, a, and a+ modes will automatically create a new file if it doesn’t<br />

yet exist; in contrast, x and x+ will throw an E_WARNING if the file already exists.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

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

Saved successfully!

Ooh no, something went wrong!