firstworks Programming with Rudiments using the standard I/O classes

Using the standard I/O classes

The filedescriptor class provides methods for generic input and output to file descriptors. Rudiments pre-defines three instances: stdinput, stdoutput and stderror, which provide access to standard input, standard output and standard error.

As these classes are ultimately instances of the filedescriptor class, many I/O methods are available, but the most commonly used are read(), write() and printf(). The printf() method features all of the formatting capabilities of the standard C printf() function.

> #include <rudiments/stdio.h>
> int main(int argc, const char **argv) {
> // standard output...
> stdoutput.write("This line is written to standard output.\n");
> stdoutput.write("Fully functional printf is also available: ");
> stdoutput.printf("%s, %c, %05d\n", "hello",'c',25);
> stdoutput.write('\n');
> // standard error...
> stderror.write("This line is written to standard error.\n");
> stderror.write("Fully functional printf is also available: ");
> stderror.printf("%s, %c, %05d\n", "hello",'c',25);
> stderror.write('\n');
> // standard input...
> stdoutput.write("Type something and hit return...\n");
> char input[1024];
> ssize_t count=stdinput.read(input,sizeof(input));
> input[count]='\0';
> stdoutput.printf("You typed: %s\n",input);
> }