2014-04-29 2 views
-1

완료되지 않은 프로젝트가 있습니다. 그것은 잘 작동하지만, 내가 만든다면, 나는 경고 얻을 :가져 오기 대신 사용해야하는 것은 무엇입니까?

the 'gets' function is dangerous and should not be used.

지금 프로젝트를 수정해야합니다,하지만 난 아무 생각이 어떻게이 기능

+2

시도 [검색 유래 (http://stackoverflow.com/questions/3302255/c을 -scanf-vs-gets-vs-fgets) 또는 [인터넷] (https://www.google.com/search?q=what+to+use+instead+of+gets&oq=what+to+use+instead + of + gets & aqs = chrome..69i57j0l2.5279j0j7 & sourceid = chrome & es_sm = 93 & ie = UTF-8). – crashmstr

답변

2

교체 사용 fgets() 온을 stdin 스트림.

gets()과 달리 fgets()은 입력 내용이 버퍼에 맞으면 줄 바꿈 문자를 제거하지 않습니다.

경우 개행 스트리핑 입력하면 같은 래퍼를 작성할 수 제공된 버퍼 넘으면 처리 동작 :

char* read_line(char* buffer, size_t buffer_len) 
{ 
    char* line = fgets(buffer, buffer_len, stdin) ; 
    char* nl = strchr(buffer, '\n') ; 
    if(nl != 0) 
    { 
     // Replace newline character with nul 
     *nl = 0 ; 
    } 
    else 
    { 
     // If buffer was shorter than the line - read and discard rest of line. 
     while(getchar != '\n') { \* do nothing*\ } 
    } 

    return line ; 
}