zend php 5 certification study guide

Page 259

240 ” Streams and Network Programming

Mode r r+ w w+ a a+ x x+

Result Opens the file for reading only and places the file pointer at the beginning of the file Opens the file for reading and writing; places the file pointer at the beginning of the file Opens the file for writing only; places the file pointer at the beginning of the file and truncate it to zero length Opens the file for writing and reading; places the file pointer at the beginning of the file and truncate it to zero length Opens the file for writing only; places the file pointer at the end of the file Opens the file for reading and writing; places the file pointer at the end of the file Creates a new file for writing only Creates a new file for reading and writing

Each of these modes can be coupled with a modifier that indicates how the data is to be read and written: the b flag (e.g.: w+b) forces “binary” mode, which will make sure that all data is written to the file unaltered. There is also a Windows only flag, t, which will transparently translate UNIX newlines (\n) to Windows newlines (\r\n). In addition, the w, w+, a, and a+ modes will automatically create a new file if it doesn’t yet exist; in contrast, x and x+ will throw an E_WARNING if the file already exists.

Licensed to 39713 - Mihails Tumkins (mihailt@gmail.com)

line using fgets(), which will continue to fetch data until it reaches a newline character. Finally, we truncate the file using ftruncate(), increment the counter and write the new counter value to the file using fwrite(). One thing to take notice of is the second argument to fopen(); this determines two things: first, whether we are reading, writing or doing both things to the file at the same time. Secondly, if the file pointer—the position at which the next byte will be read or written—is set at the beginning or at the end of the file. This flag can take on one of these values:


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.