/****************************************************************************
PROGRAMA:	files.c
AUTOR:		Kiko
FECHA:		13.10.94
FINALIDAD:	Ilustrar el uso de ficheros en bajo nivel.
HISTORIA:
BIBLIOGRAFIA:
MODO DE UTILIZACION:	-
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>					/* LibrerĦa de ficheros a bajo nivel */
#include <fcntl.h>
#include <sys\stat.h>

#define BUFF_SIZE 10

void main(void) {
	int f;								/* File handler */
	char *buffer;					/* Puntero al buffer de lectura de bytes */
	int bytes;						/* N£mero de bytes leĦdos del fichero */
	int i;

	buffer = (char *)malloc(BUFF_SIZE * sizeof(char));
	if (buffer == NULL) {
		printf("Error: No hay memoria.\n");
		exit(-1);
	}

	f = open("PRUEBA.TXT", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD);

	if (f == -1) {
			printf("Error: Imposible abrir el fichero\n");
			exit(1);
	 }

	while(1) {
		bytes = read(f, buffer, BUFF_SIZE);
		if (bytes == -1) {
			printf("Error leyendo el fichero.\n");
			exit(1);
		}

		for (i = 0; i < bytes; i++)
			printf("%c", buffer[i]);

		if (bytes != BUFF_SIZE)
			break;
	}
	close(f);
}


