Find the answer to your Linux question:
Results 1 to 4 of 4
Hello there, Thought I'd try a bit of C programming so went to the library and got 'Teach yourself C'. The first 'non-trivial program' example includes the 'gets' statement to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru fingal's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham - UK
    Posts
    1,539

    Newbie programmer - 'gets' statement


    Hello there,

    Thought I'd try a bit of C programming so went to the library and got 'Teach yourself C'. The first 'non-trivial program' example includes the 'gets' statement to receive keyboard input. It's only a program to receive your name and address: it then outputs your text to the screen.

    When I compiled this under gcc (gcc -o nameaddy nameaddress.c) the compiler told me 'gets is unsafe and should not be used'. Okay, this might be a security risk in a larger application, but I would like to know what I should use instead please
    I am always doing that which I can not do, in order that I may learn how to do it. - Pablo Picasso

  2. #2
    Linux Guru fingal's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham - UK
    Posts
    1,539
    I think I might have answered my own question by reading:

    http://crasseux.com/books/ctutorial/...and-input.html

    Apparently there are some safer Gnu alternatives to this. I think I need a better book!
    I am always doing that which I can not do, in order that I may learn how to do it. - Pablo Picasso

  3. #3
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    generally, if you want to use a gets() related function, you should use fgets()... it allows you to specify the data stream and the size of the data to be read.. check the manpage for gets()/fgets() (it's the same manpage for the whole family of functions)... i generally use scanf() or fscanf() for reading input.. but that's just me and i don't know many more I/O functions yet...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru fingal's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham - UK
    Posts
    1,539
    Okay... thanks for that. I'm writing very very basic stuff at the moment, so I'm not yet ready to share my work with the Linux community Unless they want a 'Hello Cosmos' program
    I am always doing that which I can not do, in order that I may learn how to do it. - Pablo Picasso

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •