[Get Next Line] 요구사항 및 배경지식

Soeng_dev·2021년 1월 29일
0

42 Seoul / Get_next_line

목록 보기
1/3
post-thumbnail

과제 요구사항

• Mandatory

Prototype
int get_next_line(int fd, char **line);

Turn in files
get_next_line.c, get_next_line_utils.c, get_next_line.h

Parameters
#1. file descriptor for reading
#2. The value of what has been read

Return value
1 : A line has been read
0 : EOF has been reached
-1 : An error happened

External functs.
read, malloc, free

Description
Write a function which returns a line read from a file descriptor, without the newline.

• Calling your function get_next_line in a loop will then allow you to read the text available on a file descriptor one line at a time until the EOF.

• Make sure that your function behaves well when it reads from a file and when it reads from the standard input.

• libft is not allowed for this project. You must add a get_next_line_utils.c file which will contain the functions that are needed for your get_next_line to work.

• Your program must compile with the flag -D BUFFER_SIZE=xx. which will be used as the buffer size for the read calls in your get_next_line. This value will be
modified by your evaluators and by moulinette.

• Compilation will be done this way : gcc -Wall -Wextra -Werror -D BUFFER_SIZE=32
get_next_line.c get_next_line_utils.c

• Your read must use the BUFFER_SIZE defined during compilation to read from a file or from stdin.

• In the header file get_next_line.h you must have at least the prototype of the function get_next_line.

• We consider that get_next_line has an undefined behavior if, between two calls, the same file descriptor switches to a different file before EOF has been reached on the first fd.

• lseek is not an allowed function. File reading must be done only once.

• Finally we consider that get_next_line has an undefined behavior when reading from a binary file. However, if you wish, you can make this behavior coherent.

• Global variables are forbidden.

• Bonus

• Turn-in all 3 initial files with _bonus for this part.

• To succeed get_next_line with a single static variable.

• To be able to manage multiple file descriptor with your get_next_line.

배경지식

• static 변수 (정적 변수)

» 초기화 및 저장

static 변수타입 변수이름 = 초기값(compile-time 상수로만 초기화가 가능); 형태로 초기화 되며 프로그램 실행시 단 한 번만 초기화된다. 초기값 지정이 없으면 0으로 초기화된다.

또한 정적변수를 선언한 함수(static 지역변수) 또는 프로그램(static 전역 변수)를 호출하면, 매번 정적변수에 저장되어있던 값을 불러와서 사용하고 연산된 값을 저장한다.

» 통용 범위

통용 범위는 함수 내부(static 지역변수), 프로그램 내부(static 전역변수)로 국한된다.
(static으로 선언한 변수를 extern으로 사용하려고 하면 컴파일(링크) 에러가 발생)

» 저장 장소 및 소멸 시기

저장 장소는 전역변수와 같은 정적 데이터 영역이다.
정적 데이터 영역에 저장되므로 프로그램 실행중에 항상 존재한다.
프로그램 종료와 함께 메모리에서 사라진다.

• read 함수

» prototype

ssize_t read(int fd, void *buf, size_t nbytes);parameter

» parameter

fd : 읽어들일 file descriptor
buf : read로 읽어들인 값을 저장할 buffer
nbytes : 읽어들일 바이트 수

(참고 : 매개변수(parameter)는 변수(variable)로, 전달인자(argument)는 값(value)으로 보는 것이 일반적이다.)

» 작동원리

open되어있는 file descriptor의 파일을 첫 byte부터(첫 메모리 1바이트 부터)nbytes 씩 차례로 읽어온다.

read를 여러번 호출할 경우 내부적으로 파일 내용을 읽어들인 만큼 포인터를 이동시켜서 다음 읽어들일 글자부터 차례대로 읽어온다.
(ex : 첫 호출때 3바이트 읽었다면, 4번째 바이트 부터 읽어옴)

» return value

read에 실패했다면 -1을 return

성공적으로 읽어들었다면 읽어들인 바이트 수를 return

파일의 끝에 도달했다면 0을 return

파일의 끝까지 남은 바이트 수가 nbytes보다 작다면 남은 바이트 수만큼을 return
(ex : nbytes = 10일때 남은 글자수가 7byte라면 7을 return)

profile
Software Engineer

0개의 댓글