next up previous contents
Next: The Standard Streams Up: Reference of C stdio Previous: The StdioDevice

An Example: A memory device

Figure 4: Sample Buffer I/O Driver

The example in Figure demonstrates one way to implement a device with the StdioDevice driver interface. This example uses a memory buffer as the device to be written to/read from. The code does no error checking, but does demonstrate a simple read/write interface to a memory buffer that can be manipulated by C stdio routines. For example, this code:

char space[SPACE_SIZE];
BuffDeviceType buffer (space, sizeof space);

FILE *fd = fopen("buffer", "w");
fputs("This is a string.\n", fd);

uses stdio and the above BuffDeviceType class to write a string into a stretch of memory. Of course, it would be easier to use sprintf to write into a string, but sprintf won't write into a UART, or a bus interface channel, or whatever.

Stephen Williams