/* 
	alternate.cpp:  
	
	Print every second byte from a file, using UNIX system calls 
	for file operations.

	Copyright (c) Howard J. Hamilton, September 23, 2002.
*/

#include <iostream>
using namespace std;

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{

        int fd, odd = 1;
	char c;

	/* Create the file  */
	if (argc != 2)
	{
		cerr << "Usage: alternate filename" << endl;
		exit(1);
	}

	/* Open the file for reading */
        fd = open(argv[1], O_RDONLY); 
	if (fd == -1)
	{
		perror("alternate");
		exit(2);
	}

        while (read(fd, &c, 1) > 0)
	{
		if (odd == 1)
		{
			odd = 0;
		}
		else
		{
			write(fileno(stdout), &c, 1);
			odd = 1;
		}
	}		
	close(fd);
	exit(0);
}
