Hardware Reference
In-Depth Information
Opening Files - Mode
he second argument, 'a' , speciies the mode the ile is opened with. In this program you
want to append to the ile - in other words, add to the contents already there. (If the ile
doesn't exist in the irst place, it is created.) If you wanted to replace a ile, then you should
use the write mode with an argument of 'w' . In other programs you might want to read
from a ile without changing it, and in this case you should open a ile with a mode of 'r' .
In summary, iles can be opened in three diferent modes:
r - read. he ile is read only, so you know you will not change the contents.
w - write. he ile is cleared and started from the beginning.
a - append. he ile can be added to.
Opening Files - Buffer Size
he third argument of the open function is the size of the ile bufer - that is, the number of
characters that will be grouped together before transferring them from memory onto the
computer's permanent storage. For logging birds, it is sensible to have a bufer size of 0 so
that the data is written to the SD card as soon as possible. hat way, if the power fails before
the bufer has been written out to the SD card, the least amount of data is lost.
Buffering
Buffering is a way to work more eficiently. For example, most people do not put the wash-
ing machine on for individual items of clothing; instead they collect their clothes together in
batches. This is because it takes about the same amount of time and energy to do a batch
of washing as it does for an individual item.
In the case of computers, writing characters to disk is similar; writing a few characters to a
ile takes about the same time as writing a single character. As such, the computer collects
characters together in a buffer in memory, and then writes them out (called lushing ) when
it is full. Of course, if the power is interrupted before the computer has lushed the buffer,
the data is lost.
 
Search WWH ::




Custom Search