Thread: gets() alternative

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    gets() alternative

    Im trying to get a string from the user without using scanf. Im using gets() right now but the compiler gives me a warning:

    /tmp/cchB57W8.o(.text+0x419): the `gets' function is dangerous and should not be used.

    Is there alternatives to gets()?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > Explanations of... > Why gets() is bad / Buffer Overflows

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Im using gets() right now but the compiler gives me a warning:
    Now that's a nice compiler. Make sure you read the links Dave fave you. They'll help you understand the problem and give you some other side information you need, but the main thing you need to take away: use fgets() and specify stdin as the file pointer.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    If you want to get a line of user input of inderminate length, go to the topic
    "Reading indeterminate length line from file."

  5. #5
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    makes you wonder why that function is still around. the ANSI boys should phase it out of the libraries.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the ANSI boys should phase it out of the libraries.
    It would break too much existing code, and that goes against the goals of the ISO committee. Sadly, we're stuck with gets, but we do a nice job of squelching it wherever it pops up.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alternative for fflush(stdin)
    By Raison in forum C Programming
    Replies: 11
    Last Post: 04-13-2011, 04:58 AM
  2. ASP.NET alternative to gallery.menalto.com ???
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 05-15-2005, 11:31 AM
  3. Alternative win32 compiler? And linking asm libs?
    By JMB in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2002, 01:22 PM
  4. Alternative to fstream
    By golfinguy4 in forum Windows Programming
    Replies: 3
    Last Post: 07-03-2002, 01:51 AM
  5. alternative o/s programming
    By iain in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-19-2002, 09:14 AM