11 Replies - 6445 Views - Last Post: 16 May 2013 - 11:15 AM Rate Topic: -----

#1 Praveen Kumar 28061994  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 18
  • Joined: 14-May 13

difference between getline and gets()?

Posted 14 May 2013 - 10:40 AM

it is mentioned in some sites that using gets() is not preferred
but i felt that is as useful as getline
can you kindly tell me the differences and the advantages that one has on the other
Is This A Good Question/Topic? 0
  • +

Replies To: difference between getline and gets()?

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5381
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: difference between getline and gets()?

Posted 14 May 2013 - 10:46 AM

I believe because it is used to read until a new line is entered. This allows for the ability for a string to be larger than the array defined to hold it's value.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: difference between getline and gets()?

Posted 14 May 2013 - 10:47 AM

Is this a C or C++ question?

If C the answer is to never use gets(). This function is so dangerous it has been removed from the current C standard.

If this is C++ are we talking about std::string (hopefully) or character strings? And I hope you're talking about get() not the C function gets().


Jim
Was This Post Helpful? 0
  • +
  • -

#4 Praveen Kumar 28061994  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 18
  • Joined: 14-May 13

Re: difference between getline and gets()?

Posted 14 May 2013 - 10:52 AM

can you kindly give me an example?

i am talking about c++ program and in a book it is given like
gets(string);// for the input of a string
why is it so harmful in c??? @ jim
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: difference between getline and gets()?

Posted 14 May 2013 - 11:03 AM

Read post #2. The gets() function allows any number of characters to be entered, regardless of the size of your array. This is called buffer overruns, a very bad thing. If your book is teaching the use of gets() I suggest you quickly find another book. Any modern book on either C or C++ should never use gets(), except to show why you shouldn't use this function.

Use getline().

And learn std::string it will save you a lot of troubles, especially since you have problems with pointers.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Praveen Kumar 28061994  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 18
  • Joined: 14-May 13

Re: difference between getline and gets()?

Posted 14 May 2013 - 11:08 AM

can you give me an example using std::string please @jim
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: difference between getline and gets()?

Posted 14 May 2013 - 11:13 AM

You need to start learning how to locate and understand documentation for these standard functions and classes. For example here is one example of some documentation for the std::string class.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: difference between getline and gets()?

Posted 15 May 2013 - 08:35 PM

I think one essential difference between get() and getline() is that the latter reads up to the '\n' and then discards it where as the former reads up to the '\n' and leaves it in the stream. This effectively prevents any further stream processing until the '\n' is cleared with clear() or possibly ignore().
Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: difference between getline and gets()?

Posted 15 May 2013 - 08:47 PM

heres an example of why gets is so bad
#include <stdio.h>

char *getString(char *buff) {
   return gets(buff); /* enter as many characters as you like */
}

int main() {
   char buff[5]; /* only give the input buffer 40 bytes */
   char *str = getString(buff);

   printf("You entered: %s\n", str);
   return 0;
}



Try inputting characters passed the size of the input buffer. You will over write information on getString's stack frame, possible the location where the function is supposed to return to. Do you see how that could make programs vulnerable to attacks?

There are two (obvious) ways of solving this problem.

1) Limit the number of characters the user can input
2) Move the input data away from the stack (which std::string does)

This post has been edited by jjl: 15 May 2013 - 08:50 PM

Was This Post Helpful? 1
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: difference between getline and gets()?

Posted 16 May 2013 - 04:41 AM

View PostPraveen Kumar 28061994, on 14 May 2013 - 01:40 PM, said:

it is mentioned in some sites that using gets() is not preferred
but i felt that is as useful as getline
can you kindly tell me the differences and the advantages that one has on the other


If you want to understand how getline works with a C++ string ...

you can think of it like this:

it reads the whole line and discards any newline char(s) at the end
into a dynamically allocated C++ string of just the right size to hold the line (at run time)
which string memory is freed up when the string goes out of scope


but ...

gets uses a C string buffer that has memory of pre-fixed size, and that size was specified at compile time.
So ... if the line you are reading has more bytes in it than the C string pre-fixed size buffer can hold ...
it will OVERFLOW the buffer as already mentioned above.

About the only thing the same between
gets (In C)
and
getline( in C++ )
is that they both will 'eat' the newline char(s) at the end of the line (if there).

If you need to use C strings, and you wish to use a function in C like getline (getline is in C++ AND NOTE: you can't use C strings with getline ... getline is a function that returns a string of just the right (dynamic memory) size to hold a line being read in)

you can code your own function to do that in C style code ...
or use a function like my readLine freely available in the DIC snippets
or at this link:

readLine.h
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,276
  • Joined: 25-December 09

Re: difference between getline and gets()?

Posted 16 May 2013 - 04:55 AM

Quote

AND NOTE: you can't use C strings with getline ...

Sure you can, as long as you use the proper version of getline(). There are two versions of getline() one for use with std::string and one for use with C-strings.

The thing to remember with C-strings in to never use a function where you don't limit the number of characters you can retrieve into the string. This includes the scanf() family and the C++ extraction operator>>. These methods have ways to limit the number of characters you accept into the string to avoid buffer overruns, the width modifier and the setw() manipulator.

Quote

There are two (obvious) ways of solving this problem.

1) Limit the number of characters the user can input
2) Move the input data away from the stack (which std::string does)


Really there is only one safe solution. Which you list is reason 1. Just using dynamic memory doesn't actually solve the buffer overrun problem, it just moves the problem from the stack to the heap. Buffer overflow in either the stack or heap is a problem that must be repaired.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: difference between getline and gets()?

Posted 16 May 2013 - 11:15 AM

View Postjimblumberg, on 16 May 2013 - 07:55 AM, said:

Quote

AND NOTE: you can't use C strings with getline ...

Sure you can, as long as you use the proper version of getline(). There are two versions of getline() one for use with std::string and one for use with C-strings.

The thing to remember with C-strings in to never use a function where you don't limit the number of characters you can retrieve into the string. This includes the scanf() family and the C++ extraction operator>>. These methods have ways to limit the number of characters you accept into the string to avoid buffer overruns, the width modifier and the setw() manipulator.

Quote

There are two (obvious) ways of solving this problem.

1) Limit the number of characters the user can input
2) Move the input data away from the stack (which std::string does)


Really there is only one safe solution. Which you list is reason 1. Just using dynamic memory doesn't actually solve the buffer overrun problem, it just moves the problem from the stack to the heap. Buffer overflow in either the stack or heap is a problem that must be repaired.

Jim



Thanks for all the clarifications, Jim ... I forgot about the C string versions of getline - I never use them, is probably why ;(

But the OP needs to hear the truth ... Thanks for bringing it forward :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1