Questions tagged [gets]

Anything related to C or C++ standard library functions gets (C) or std::gets (C++). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. DO NOT USE THESE FUNCTIONS: they are deprecated, and since C11 gets is no longer part of the standard.

Anything related to C or C++ standard library functions gets (defined in <stdio.h> C standard header) or std::gets (defined in <cstdio> C++ standard header). These functions are used to read a sequence of characters from an input stream and to write it into a character buffer as a string. There are no constraints on the functions to prevent them writing outside the bounds of the array that is passed.

DO NOT USE THESE FUNCTIONS: they are dangerous and deprecated for security reasons because they are susceptible for easily causing buffer overflow. For more information about why the use of gets is harmful, consider the link below.

Since C11 (ISO/IEC 9899:2011) gets has been removed from the C standard library. Annex K of the C standard defines an optional replacement function called gets_s for backwards-compatibility reasons, but makes the following recommendation to use fgets whenever possible:

ISO 9899:2011 K.3.5.4.1

Recommended practice

The fgets function allows properly-written programs to safely process input lines too long to store in the result array. In general this requires that callers of fgets pay attention to the presence or absence of a new-line character in the result array. Consider using fgets (along with any needed processing based on new-line characters) instead of gets_s.

Note that gets_s() is not generally available except on Windows using the Microsoft C library.

See CPPreference.com:

422 questions
2
votes
1 answer

Are there any substitutes for the gets() function in c99?

Why is implicit declaration of gets() not allowed in C99? I had referred this question, which was how I came to know about it's deprecation.
2
votes
1 answer

How can I fix the problem with gets() function in C?

There is a problem with gets function. The first gets I write does not work but the ones comes next works properly. I put an extra gets() function at the beginning, program just skips it and gets the string I want. But it is not safe and reliable.…
Emre Turan
  • 83
  • 1
  • 8
2
votes
7 answers

gets() problem in C

I wrote the following code: #include #include #include #define SIZE 128 int main () { char mychar , string [SIZE]; int i; int const count =0 ; printf ("Please enter your string: \n\n"); …
Batman
  • 1,244
  • 2
  • 14
  • 26
2
votes
0 answers

Can I run GETS in a Ruby script run by my Vagrantfile

I am new to Vagrant and Ruby. Is it possible to get user keyboard input into a ruby script I am triggering from a Vagrantfile thus : config.trigger.after [:provision] do |trigger| trigger.run = {inline:…
Neil E
  • 43
  • 6
2
votes
3 answers

How can I navigate through an array of strings of any length in C?

Understanding handling direct pointers in C Here is a code that works for an array of strings for fixed number of items and fixed line length : #include #include #include #define MAXNAMELEN 100 #define MAXLINELEN…
Akshaya
  • 53
  • 5
2
votes
2 answers

Why can't the first string element be accessed if a limit is read using scanf() in c

int main(){ char str[10][50],temp[50]; int lim,i,j; printf("Enter lim: "); scanf("%d",&lim); for(i=0;i
stackphish
  • 143
  • 3
  • 8
2
votes
2 answers

How to test REST API google maps?

I want to test the google maps API so I can see the resources that the API have, i found the SoapUI REadyAPI, but I'm still confused. Is there a way to make this REST API calls (gets) from console so i can see the JSON of Google maps? Thanks in…
Rorriizz Gold
  • 25
  • 1
  • 5
2
votes
1 answer

Cryptic Ruby syntax for averaging ASCII values

I saw an intriguing answer to a simple question. The question was: "Get the average of the ASCII values of each character in a string read from gets" One of the correct answer was: p gets.sum/~/$/ I searched Google but couldn't find any explanation…
frank
  • 295
  • 3
  • 8
2
votes
0 answers

Difference scanf vs gets vs fgets in terms of efficiency

What is the difference between scanf, gets and fgets (and others?) in terms of efficiency? Example case: imagine reading in a list of several thousand integers (per line) This question has been asked more often, but everyone always responds by…
Rick
  • 308
  • 3
  • 8
2
votes
1 answer

Fastest way to read doubles from file in c++

I have a big file with real numbers, and I need to read them into doubles the fastest possible way. I can chose the format of the file myself ( 1 number a line, or multiple for example) tried scanf, it seems slow, tried gets(s) and then parsing…
Herokiller
  • 2,891
  • 5
  • 32
  • 50
2
votes
3 answers

gets() taking input without actually giving it any input?

I'm fairly new to C so sorry if this is a stupid question but when I run the following code: #include int main () { int i; int test[10]; char string[81]; for(i = 0; i < 10; i++){ scanf("%d", &test[i]); } …
JimR
  • 2,145
  • 8
  • 28
  • 37
2
votes
3 answers

C: Reading an optional input from console

The user may use 2 commands: move black (or) move So the 'black' part is optional. I know that the user input is confined to 50 characters top, so I can use scanf() to read each string by itself. However, I cannot use 3 times scanf() as for the…
din
  • 25
  • 7
2
votes
1 answer

How to overwrite the current console line after gets in Ruby?

I have this piece of code: def wait_for_input regex print "> ".red someInput = gets while (regex =~ someInput).is_a? NilClass do print "\r> ".red someInput = gets STDOUT.flush end someInput end The carriage return…
Nomid
  • 91
  • 8
2
votes
5 answers

Error in getting input in structure using gets()

I am making a program to take input in teacher structure but there is unknown run time error , here is the code - #include #include struct Teacher { char Name[30]; char Qualifications[20]; int experience_year; }th[10]; void…
udit043
  • 1,610
  • 3
  • 22
  • 40
2
votes
1 answer

gets_s giving error with gcc

gets_s() function is not working on my compiler. I am using codeblocks with GCC. This is the error : undefined reference to '_imp_gets_s. Can anyone please tell me how to resolve this error.
jashan
  • 41
  • 5