1.커멘드라인 인자를 다루는 방법
예제)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
/* | |
argc : 실행인자 개수 | |
argv[] : 실행인자의 내용(배열형태) | |
exit(0) : 프로그램 종료 <stdlib.h> | |
*/ | |
int main (int argc, char *argv[]){ | |
int i; | |
printf("argc=%d\n" , argc); | |
for (i=0; i<argc; i++) { | |
printf("argv[%d] = %s\n" , i , argv[i]); | |
} | |
exit(0); | |
} |
결과)

2.파일을 읽어서 표준출력에 표시하는 방법
예제)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
//파일을 읽어서 표준출력으로 내보내는 함수 | |
static void do_cat(const char *path); | |
//에러 처리 함수 | |
static void die(const char *s); | |
int main (int argc, char *argv[]){ | |
int i , ii; | |
//인자 개수 | |
printf("argc(인자총개수)=%d\n" , argc); | |
//인자내용 출력 | |
for (i=0; i<argc; i++) { | |
printf("argv[%d](인자내용) = %s\n" , i , argv[i]); | |
} | |
//전달된 인자의 개수가 없다면 에러 처리 | |
if(argc < 2){ | |
printf("%s : file name is not given\n", argv[0]); | |
exit(1); | |
} | |
//파일 내용은 인자의 1번째 부터 존재한다. 0번째는 인자의 개수가 담겨있음 | |
for (ii = 1; ii < argc; ii++) { | |
printf("%s 파일내용 출력 시작! \n" , argv[ii]); | |
do_cat(argv[ii]); | |
} | |
exit(0); | |
} | |
#define BUFFER_SIZE 2048 | |
static void do_cat(const char *path){ | |
//파일 디스크립터: 커널이 스트림을 열때 부여하는 번호. | |
//프로세스에서 파일을 읽거나 쓸때, 혹은 다른 프로세스와 데이터를 주고 받을때사용. | |
int fd; | |
//읽은 파일을 담을 변수 | |
unsigned char buf[BUFFER_SIZE]; | |
int n; | |
printf("do_cat - 받은 파일 경로 : %s \n" , path); | |
//파일을 읽기 전용으로 연다. | |
fd = open(path,O_RDONLY); | |
printf("파일 open fd : %d \n" , fd); | |
//파일 디스크립터가 0보다 작으면 에러 발생 | |
if (fd < 0) die(path); | |
for (;; ) { | |
printf("* fd : %d, size of buf : %lu \n" , fd , sizeof buf); | |
//파일 데이터 읽기(파일 디스크립터, 읽은 파일을 담을 변수, 한번에 읽을 크기) | |
n = read(fd, buf, sizeof buf); | |
printf("*n 의 값 : %d \n" , n); //읽은 파일의 바이트 수 | |
printf("*buf 담긴 값 : %s \n" , buf); //읽은 실제 값 | |
if(n < 0)die(path); | |
//읽은 바이트 수가 0, 파일의 끝에 도달하여 더 읽어들일 바이트가 없는지 확인하는 조건문. | |
if(n == 0) break; | |
if (write(STDOUT_FILENO, buf, n) < 0) die(path); | |
} | |
if (close(fd) < 0) die(path); | |
} | |
static void die(const char *s){ | |
printf("die 함수 진입: %p \n" , s); | |
//표준에러 출력 | |
perror(s); | |
exit(1); | |
} |
결과)

hello 파일에 있는 "hello my name is kang" 값이 터미널에 출력되었다. 그 후 for문 안에서 n의 값, 즉 파일을 읽어 들인 값이 없기 때문에 더 이상 파일을 읽어들이지 않고 종료된다.
연습문제
*위에서 작성한 ./b 명령어를 수정하여 실행 인자가 없는 경우에는 표준 입력에서 읽도록 수정하라.
*\n의 개수를 세어서 파일이 몇 줄로 구성되었는지 출력 하는 명령어를 작성하라 (wc-l 과 동일 기능)
참고: 모두를 위한 리눅스 프로그래밍
'컴퓨터 기초 > 운영체제 실습' 카테고리의 다른 글
[운영체제 실습] 6.파일과 디렉토리 (0) | 2020.06.25 |
---|---|
[운영체제 실습] 5.파일 다루기(라이브러리) (0) | 2020.06.23 |
[운영체제 실습] 4.에러처리 및 파일 다루기(low level) (0) | 2020.06.23 |
[운영체제 실습] 2.파일 쓰기와 파일 읽기 (0) | 2020.06.16 |
[운영체제 실습] 1.프로세스 생성과 종료 (0) | 2020.06.03 |