readline

raejun·2021년 9월 3일
0

readline 사용하기

// main.c
#include <stdio.h>
#include <stdlib.h>

// 컴파일시 readline이라는 라이브러리를 추가해야 사용 가능
#include <readline/readline.h>
#include <readline/history.h>

int main(void)
{
	char *str;
    
    while (1)
    {
    	str = readline("prompt: "); // 입력한 문자열 주소를 저장
        if (str)
        	printf("%s\n", str);
        else
        	break; // EOF 일때 탈출
        add_history(str); // 출력한 문자열을 저장하여 방향키 up, down으로 확인 가능
        free(str); // realine은 힙에 저장되어 메모리 해제
    }
    return (0);
}
  • gcc main.c -lreadline
profile
정리노트

0개의 댓글