Previous: The Powers Of Two, Up: File Library Examples


6.8.3.2 Words Reversal

We will now proceed with something that many will find disturbing: we will reverse the words within lines. And to make this project even more challenging we shall create a filter, one that filters the standard input on the standard output.

First, we declare two files, one for input, one for output, and some list (we deploy the `f_list' function):

file source, target;
list l;

Open that standard input:

f_open(source, "/dev/stdin", OPEN_READONLY, 0);

And that standard output:

f_open(target, "/dev/stdout", OPEN_WRITEONLY, 0);

And waste no time in reversing those words, all of them:

while (f_list(source, l, 0) != -1) {
    mode i;

    i = l_length(l);
    while (i) {
	if (i != l_length(l)) {
	    f_byte(target, ' ');
	}

	i = i - 1;

	f_text(target, l_q_text(l, i));
    }

    f_byte(target, '\n');
}

We bother to close output:

f_close(target);

And input:

f_close(source);

Now, that's programming!