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
3
votes
1 answer

How to make gcc 4.7 warn about use of the infamous gets() function?

I saw yet another question about C where the code was using gets(), and I commented with the usual warning about never using gets() except when you want to demonstrate how to break security. This time, I decided to check if my compiler issued a…
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
3
votes
2 answers

What does gets() save when it reads just a newline

Here's the description of gets() from Prata's C Primer Plus: It gets a string from your system's standard input device, normally your keyboard. Because a string has no predetermined length, gets() needs a way to know when to stop. Its method is…
User314159
  • 7,733
  • 9
  • 39
  • 63
3
votes
1 answer

Provide hex value as an input to gets in C

I'm working on a simple arc injection exploit, wherein this particular string gives me the desired address of the place where I'd like to jump: Á^F@^@. This is the address 0x004006c1 (I'm using a 64 bit Intel processor, so x86-64 with little endian…
user2841250
  • 143
  • 1
  • 5
3
votes
2 answers

gets.chomp without moving to a new line

I understand about the \n that's automatically at the end of puts and gets, and how to deal with those, but is there a way to keep the display point (the 'cursor position', if you will) from moving to a new line after hitting enter for input with…
jeremiah
  • 49
  • 1
  • 6
3
votes
3 answers

How appropriate is the use of the statement fflush(stdin)?

To read multi-word strings, I have been using the gets() function. The behaviour of the gets() function is unpredictable to me and I use the statement fflush(stdin) before every gets() statement to avoid issues. Is using this statement this way…
amulous
  • 704
  • 2
  • 6
  • 15
3
votes
1 answer

The Ruby gets method truncates my input at 256 characters

I am trying to enter a long string (> 256 characters but generally < 512) in Ruby with: puts "Enter long string" ilogo = gets.chomp puts "#{ilogo}" For some silly reason, it always truncates the input to keep only 256 characters (before chomp). I…
jen
  • 300
  • 1
  • 3
  • 11
3
votes
1 answer

Why does gets return zero in Ruby

I have a simple method def save_logline print "What's the name of the movie" movie_name = gets.strip print "And what is your precious logline?" logline = gets.strip File::open(movie_name + '.txt', 'w') do |f| f.write(logline) …
thank_you
  • 11,001
  • 19
  • 101
  • 185
3
votes
2 answers

gets() only taking input once in while loop

I am new to C and working through some exercises, but having trouble with gets() in a while loop. In searching, I believe that it may have something to do with the \n character, but I was hoping that someone would be able to give me a more thorough…
Gitarooman
  • 97
  • 1
  • 2
  • 9
3
votes
5 answers

Dynamic Memory allocation for char pointer

I have a strange issue related to dynamic memory allocation to char pointer. I have something like char *input = new char; //1 gets(input) //2 char *dest = new char; //3 during the step3, i get heap corruption error during runtime. This only…
Kartikkumar Rao
  • 171
  • 2
  • 18
2
votes
1 answer

read an unknown number of lines

I need to implement in C the program ,which reads an unknown number of lines from stdin. I know that the maximum number of lines is 100. I tried to use gets ,but I don`t know when to stop the loop. Can you advise me how to implement it?
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
2
votes
2 answers

TCL gets command with kind of -nohang option?

Here is a code which just implements an interactive TCL session with command prompt MyShell >. puts -nonewline stdout "MyShell > " flush stdout catch { eval [gets stdin] } got if { $got ne "" } { puts stderr $got } This code prompts MyShell >…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
2
votes
5 answers

Is gets() considered a C function or a C++ function?

#include using namespace std; void main(){ char name[20]; gets(name); cout<
Wizard
  • 10,985
  • 38
  • 91
  • 165
2
votes
1 answer

Trying to search a string for a gets input in ruby

I am having issues searching for a string input by the user using gets. Here's the outline: puts "What are we searching for?" search = gets z=1 result = [] file = File.open('somefile.txt', 'r+') file.each do |line| if line.include?(search) …
SullX
  • 214
  • 4
  • 17
2
votes
4 answers

How to write the code correctly in Ruby? So that it produces the correct output?

I want to do something like that. puts "Please write your age: " age = gets.chomp if #{age}<18 puts "you are illegal" else #{age}>18 puts "You are legal" end the output i get is: "Please write your age" 15. you are illegal you are legal" and…
eb1212
  • 23
  • 3
2
votes
3 answers

puts(), gets(), getchar(), putchar() function simultaneously use in the program

I have a confusion related to using puts(), gets(), putchar() and getchar() simultaneously use in the code. When I have run the below code, it is doing all steps: taking the input, printing the output, again taking the input, printing the…
Priyanka
  • 21
  • 4