/****************************************************
	Jim Mitchell (mitchell@rpi.net.au)
	
	- Simple Program that opens and reads a file
****************************************************/

#include <fcntl.h>
#include <unistd.h>
#define BUFFER 1024

main(int argc, char **argv){
	int fd ;//File Descriptor
	ssize_t nread;
	char buf[BUFFER];
	
	/* Check to see if command and filename */
	if (argc != 2) {
		printf("Usage: openfile filename\n\n");
		exit(1);
	}else{
		/* open file from command line for read only access */
		fd = open(argv[1], O_RDONLY);
		
		if (fd < 0){
			printf("File Descriptor Error\n\n");
			exit(1);
		}
			
		/* Read from the file the number of Bytes BUFFER and put 
		into the array buf. nread is number of bytes read*/
		nread = read(fd, buf, BUFFER);
		
		/* print buf array */
		printf("%s\n\n", buf);
		
		/* print number of bytes read */
		printf("Number of Bytes read: %i\n", nread);
		
		/*close file descriptor*/
		close(fd);
		
	}
}
