3

I am using Visual Studio 2015 and I want to write a code regarding files in C language. I want to get a name from the keyboard (and then put this name in the file) and I am using the function gets. But it doesn't work, the compiler says ,,gets is undefined". Can anybody help me, please?

  • 1
    Can you show us your code? – Haris Apr 10 '16 at 18:50
  • 1
    1.) Be aware, gets() is [deprecated](http://www.crasseux.com/books/ctutorial/gets.html) – Enkelli Apr 10 '16 at 18:52
  • 1
    From [msdn](https://msdn.microsoft.com/en-us/library/2029ea5f.aspx?f=255&MSPPError=-2147217396): "These functions are obsolete. Beginning in Visual Studio 2015, they are not available in the CRT. The secure versions of these functions, gets_s and _getws_s, are still available." – Enkelli Apr 10 '16 at 18:57
  • 4
    `gets` isn't just deprecated. As of C11 the deprecation ran out. It is completely *eliminated* from the standard library. Any toolchain half-attempting to comply with the C11 standard should no longer offer its services (as you're seeing). – WhozCraig Apr 10 '16 at 18:57
  • @WhozCraig Indeed. Per paragraph 6 of the **Foreward**: *- removed the `gets` function (``)* (top of page xiv, C Standard link: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Andrew Henle Apr 10 '16 at 19:45
  • Two more points: (1) Whatever book or tutorial or greybeard instructor it was who told you about `gets` in the first place is seriously out-of-date, and you will want to view any other teachings of theirs with suspicion. (2) Your next question, after switching to `fgets`, is likely to be, "But what abut the newline?" See [here](https://stackoverflow.com/questions/2693776). – Steve Summit Jul 01 '21 at 13:01

1 Answers1

2

The comments have pointed out that gets is taken out completely. The reason is that gets can easily cause buffer overflow.

You are supposed to use fgets instead since you can limit the input to the size of your buffer.

Community
  • 1
  • 1
user3813674
  • 2,553
  • 2
  • 15
  • 26