Next: print.c Up: Program Listings Previous: bitcount.c

lowio.c


/*********************************************** lowio.c ********/

#include <fcntl.h>
#include <stdio.h>
#define PERMS 0600	/* r,w permission owner only  (octal no.)*/

void inputtext (char *buf, int fd);
void display (char *buf, int fd);

main () {
    char    buf[BUFSIZ];
    int     fd1,
            fd2,
            t;

    if ((fd1 = creat ("iotest", PERMS)) == -1) {
	printf ("Cannot open file with creat\n");
	exit (1);
    }

    inputtext (buf, fd1);

    close (fd1);

    if ((fd2 = open ("iotest", 0, O_RDONLY)) == -1) {
	printf ("Cannot open file\n");
	exit (1);
    }
    display (buf, fd2);
    close (fd2);
}

void inputtext (char *buf, int fd1) {
    register int    t;

    printf ("Enter lines of text, end with quit\n");
    do {
	for (t = 0; t < BUFSIZ; t++)
	    buf[t] = '\0';
	gets (buf);

	if (write (fd1, buf, BUFSIZ) != BUFSIZ) {
	    printf ("Error in writing\n");
	    exit (1);
	}
    } while (strcmp (buf, "quit"));
}

void display (char *buf, int fd2) {
    for (;;) {
	if (read (fd2, buf, BUFSIZ) == 0)
	    return;
	printf ("%s\n", buf);
    }
}


Dave.Marshall@cm.cf.ac.uk
Wed Sep 14 10:06:31 BST 1994