Previous: , Up: File Library Samples   [Index]


7.11.3.2 Words Reversal

We will reverse the words within lines. To make this project 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) {
    integer i;

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

	i = i - 1;

	f_text(target, l[i]);
    }

    f_byte(target, '\n');
}

We bother to close output:

f_close(target);

And input:

f_close(source);

Now, that’s programming!