본문 바로가기

System Programmings/C

[C] open()

반응형
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
	int fd;
	int cnt=0;
	char buf[100]

	if(argc !=2){
		printf("No File\n");
		return 0;
	}

	printf("Input file : %s\n", argv[1]);

	fd = open(argv[1], O_RDONLY);
	
	if(fd<0)
	perror("open()");
	else{
		printf("FD = %d\n", fd);
	}
	
	buf[0]='\0';
	cnt=read(fd, buf, '\n');
	printf("cnt = %d\n",cnt);
	
	buf[cnt]='\0';
	printf("File Contents = %s\n", buf);

	return 0;
}


반응형