4 Replies - 536 Views - Last Post: 05 January 2013 - 05:12 AM Rate Topic: -----

#1 barcodelll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-December 12

gets command in c

Posted 04 January 2013 - 10:01 PM

hello, i want to ask about the "gets" command (which has the same function as scanf). i use the ubuntu 11.10 and has no problem in the program with the "gets"...

but, when i try it in another pc with ubuntu 10.10 (if i'm not wrong, it has a different environment with ubuntu 11.10).
my question is: is that true? i mean, is that true that the gets command doesn't work in ubuntu 10.10? and is there any subtitution? and is there any advice?

thanks in advance \:D/

Is This A Good Question/Topic? 0
  • +

Replies To: gets command in c

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,104
  • Joined: 27-December 08

Re: gets command in c

Posted 04 January 2013 - 10:47 PM

You really shouldn't be using gets(), as it doesn't allow you to specify a buffer length. Just use scanf() instead.

As per Beej's Guide: http://beej.us/guide...ipage/gets.html
Was This Post Helpful? 0
  • +
  • -

#3 barcodelll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-December 12

Re: gets command in c

Posted 04 January 2013 - 11:10 PM

View Postmacosxnerd101, on 04 January 2013 - 10:47 PM, said:

You really shouldn't be using gets(), as it doesn't allow you to specify a buffer length. Just use scanf() instead.

As per Beej's Guide: http://beej.us/guide...ipage/gets.html


but in my program, the user should be able to write several words which separated with space, and i've tried with scanf, and then the words doesn't comes up as it should be.

for example: (with scanf)

input for var1: asdf ghjk
output var1: asdf
output var2:ghjk

and i've asked about this, and they said that scanf doesn't allowed the input using space. it must be a single word.

but thanks \:D/
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10565
  • View blog
  • Posts: 39,104
  • Joined: 27-December 08

Re: gets command in c

Posted 04 January 2013 - 11:15 PM

With scanf(), you can read in multiple words, but you have to specify that with the string formatter- one per word. You can also use getc() and pass it stdin. The idea with getc() is that you malloc() a buffer, and you would realloc() a bigger buffer when it fills up. Repeat this until you've read in the string in its entirety.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: gets command in c

Posted 05 January 2013 - 05:12 AM

View Postbarcodelll, on 05 January 2013 - 06:01 AM, said:

my question is: is that true? i mean, is that true that the gets command doesn't work in ubuntu 10.10?


No, that's not true.

View Postmacosxnerd101, on 05 January 2013 - 06:47 AM, said:

You really shouldn't be using gets(), as it doesn't allow you to specify a buffer length. Just use scanf() instead.


Given that scanf doesn't really do the same thing as gets, I'd rather use fgets instead, which does take a length.


View Postmacosxnerd101, on 05 January 2013 - 07:15 AM, said:

With scanf(), you can read in multiple words, but you have to specify that with the string formatter- one per word.


I think he wants to read an arbitrary amount of words, i.e. a whole line.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1