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

gets function in C not waiting for input

I have a question i want to ask about the gets function. I am currently writing a program of 2 matrix addition and I'm confused about the gets function behavior. From what i read from the description from tutorialspoint about the gets() function in…
Chamoileon
  • 23
  • 5
2
votes
1 answer

Undefined reference to gets_s

So basically I'm learning C and they taught us to use the gets() function, which I found that it was removed and cannot be used anymore. So I'm trying to learn to use the alternative, the gets_s() function, but I can't seem to. For example the…
Osama Qarem
  • 1,359
  • 2
  • 13
  • 15
2
votes
1 answer

What should I use, instead of gets?

There is a finished project, which is not mine. It works well, but if I build, I get a warning: the 'gets' function is dangerous and should not be used. I have to now fix the project, but I have no idea, how to replace this function
Iter Ator
  • 8,226
  • 20
  • 73
  • 164
2
votes
6 answers

How do you use sets and gets in C++?

I've used them in java and didn't seem to have too many issues, but I'm not grasping them very well in C++. The assignment is: Write a class named Car that has the following member variables: year. An int that holds the car's model year. …
Soully
  • 859
  • 6
  • 16
  • 24
2
votes
3 answers

Weird scanf behaviour when reading number and newline

I just realize this 'bug' of scanf now after 8 years with C. Below scanf code will skip the leading whitespace characters from the second line of input. int x; char in[100]; scanf("%d\n",&x); gets(in); Input: 1 s x will contain 1, but in…
dragon135
  • 1,366
  • 8
  • 19
2
votes
4 answers

Is character array in C dynamic?

I have written a simple program in C. A program to input a String and display it along with the length. #include int main() { char a[4]; printf("Enter the name : "); gets(a); printf("\nThe name enterd is : %s",a); printf("\nLength of…
MELWIN
  • 1,093
  • 4
  • 12
  • 19
2
votes
1 answer

gets function is not reading my variable

I noticed today that sometimes when I use the gets function my compiler simply ignores it. OK. This is an example where gets works: #include void main() { char s[50]; gets(s); puts(s); } Now if I make this simple change to my…
2
votes
5 answers

C structure not scanning all the inputs

I have this C code: #include "stdio.h" main() { struct books { char name[100],author[100]; int year,copies; }book1,book2; printf("Enter details of first book\n"); gets(book1.name); gets(book1.author); …
user2675010
2
votes
1 answer

How to use "gets" function in C++ after previous input?

I tried to input data with gets() function, but whenever program execution get to the the lien with the gets, it ignores it. When I use gets() without previous data input, it runs properly. But when I use it after data input the problem…
user2355950
2
votes
2 answers

Usage of two gets() in C++

I am learning about classes in C++. I made a simple program using the concept of classes. In the program I need the person to enter the details of the book. Here is that function: void Book::add(){ cout << "Enter name of Book:…
Vijay
  • 77
  • 1
  • 10
2
votes
3 answers

gets() does not work

I have a program written in C and it calls gets() from a switch when a user chooses the option of 3. Here is my code. It does not seem to wait to wait for the user to input something. Rather the program continues in the switch. void…
phil
2
votes
1 answer

My layout gets messed up when zoomed out

I started learn webdesign one week ago. I'm trying to create a webpage with three divs next to each other. My layout gets messed up when zoomed out. 50% zoom level is okay but when I started to zoom further right sidebar goes under the content div.…
2
votes
1 answer

How to set a predefined input

I want a simple pre-defined input in ruby. What I mean is that I want something to be there by default so the user can edit or just simply press Enter to skip. I'm using STDIN.gets.chomp. not predifiend : "Please enter a title: " predefined :…
Kivylius
  • 6,357
  • 11
  • 44
  • 71
2
votes
3 answers

Dev-C++ Input skipped

#include #include main() { int i; char c, text[30]; float f; printf("\nEnter Integer : "); scanf("%d",&i); printf("\nEnter Character : "); c = getch(); printf("\nEnter String:"); …
Ravitheja
  • 142
  • 1
  • 8
2
votes
3 answers

Ruby File gets not reading content after last blank line \n

I'm trying to write a very simple ruby script that opens a text file, removes the \n from the end of lines UNLESS the line starts with a non-alphabetic character OR the line itself is blank (\n). The code below works fine, except that it skips all…
ShawnyV
  • 23
  • 5