2

There is a finished project, which is not mine. It works well, but if I build, I get a warning:

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

I have to now fix the project, but I have no idea, how to replace this function

Iter Ator
  • 8,226
  • 20
  • 73
  • 164
  • 2
    Try [searching stackoverflow](http://stackoverflow.com/questions/3302255/c-scanf-vs-gets-vs-fgets) or [the internet](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 Apr 29 '14 at 18:10

1 Answers1

4

Use fgets() on the stdin stream.

Note that unlike gets(), fgets() will not remove the newline character at the end of the input if it will fit in the buffer.

If stripping the newline and handling behaviour when input exceeds the provided buffer you might write a wrapper such as:

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 ;
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Hi Clifford, I'm trying to understand - is there a reason it fills the buffer AND returns the line? Could the function be written to return void? – Vee Aug 28 '18 at 15:45
  • 1
    @Grandclosing : Of course, but returning a value costs nothing and allows the function call to be used as an argument directly e.g: `process_line( read_line( buffer, sizeof(buffer) ) ;` - if you don't want to use the return value, just ignore it - it need not be assigned to anything or used an argument. For example when was the last time you used the return value from `printf()`? Or even noticed it had one!? – Clifford Aug 28 '18 at 16:14
  • Good point there! I just looked up printf, I can't believe I never noticed that. Thanks! – Vee Aug 28 '18 at 16:36
  • The condition in the `while` seems to be wrong :D – Antti Haapala -- Слава Україні Aug 09 '20 at 09:15
  • @AnttiHaapala : Only 6 years to spot that! – Clifford Aug 09 '20 at 11:06
  • Well, now that I think about it, strange things can happen if last line has more than buffer_len - 1 characters and there is no line terminator on the last line :D – Antti Haapala -- Слава Україні Aug 09 '20 at 11:46
  • Sorry if I didn't understand this, but how does line get returned with newline removed ? Does it happen at *nl = 0 ? Also, what does "If buffer shorter than line mean ? Thank you in advance, I am trying to understand for the C class. – SunnyAk Apr 19 '22 at 04:52
  • 1
    @SunnyAk : re. Removing the newline; exactly as the comment states. Regarding the buffer length read the library docs for `fgets`; if no newline is buffered, the input was longer than the buffer length, and the input buffer needs emptying. – Clifford Apr 19 '22 at 06:19