Here's a complete example of reading the file using the low-level functions you are required to use.
Replace the comment /* Process the data */
with your own code that does something useful with the data you read.
int rfd; /* File descriptor. */
char buffer[BUFFER_SIZE]; /* Buffer to put file content into */
int bufferChars; /* number of characters returned by the read function */
/* Open the file */
if ((rfd = open(argv[1], O_RDONLY, 0)) < 0)
perror("Open failed.");
/* Read and process the file */
while (1)
{
/* Normal case --- some number of bytes read. */
if ((bufferChars = read(rfd, buffer, BUFFER_SIZE)) > 0)
{
/* Process the data */
}
else if (bufferChars == 0) /* EOF reached. */
break;
else /* bufferChars < 0 --- read failure. */
perror("Read failed.");
}
close(rfd);