logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/feb01/read.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define K 1024

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

	if (argc < 2){
	   fprintf(stderr, "Usage %s file\n",argv[0]);
	   exit(1);
	}

	char data[K];
	int rt;

	//open a file
	int fd = open(argv[1], O_RDONLY);

	if(fd == -1){
		write(2, "open failed\n",12);
		return -1;
	}

	rt = read(fd, data, K);

	printf("Number of bytes read: %d\n", rt);


	close(fd);
	
	return 0;

}