What alternative can I use instead of gets() and puts()?

alternative to gets in c
getline c
gets' is deprecated
gets in c
warning: this program uses gets(), which is unsafe
getline in c++
is fgets safe

Code snippet for gets()

int main(int argc, char *argv[])
    char str[MAX_SIZE]

Code snippet for puts()

  printf("The most probable key is: %d\n", max2);
  return 0;

I did not paste my whole code since it seems pretty irrelevant for my question. I just want to know a way I could fix this because when I run my code through GCC, it gives me errors and is not allowing me to use gets(). How would I fix this?

Use fgets and fputs instead. There is - beside removing all the flaws of gets- also a main difference in the usage between gets and fgets: fgets stores a new line character in the buffer (and gets does not).

So an equivalent - i.e. eliminating any new line if not desired - could look as follows. Function call strcspn(buffer,"\n") gives the longest sequence of characters until a new line is encountered (or strlen(buffer), if the string does not contain a new line). By writing a 0 to the index at this position a new line - if any - is eliminated.

char buffer[MAX_SIZE] = "";
if (fgets(buffer, MAX_SIZE ,stdin)) {
  buffer[strcspn(buffer,"\n")] = '\0';

What could be the best alternative of gets so as to take a string (that , Program using gets()? Alternative function to gets() is fgets() and getline(). fgets () can be used in place of gets() to solve the problem. You can use scanf statements with appropriate delimiter. For eg you want the string to terminate on seeing newline then do as follows Scanf(“%[^ ]s”,str); Here str is the name of you're string.

How Dangerous is using gets() in c and with alternative solution , as gets() doesn't do any array bound testing. gets() keeps on reading until it sees a newline character. To avoid Buffer Overflow, fgets() should be used instead  For reading a string value with spaces, we can use either gets() or fgets() in C programming language. Here, we will see what is the difference between gets() and fgets(). fgets() It reads a line from the specified stream and stores it into the string pointed to by str.

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead char * fgets ( char * str, int num, FILE * stream );

The following example shows the usage of fgets() function.

#include <stdio.h>

int main () {
   FILE *fp;
   char str[60];

   /* opening file for reading */
   fp = fopen("file.txt" , "r");
   if(fp == NULL) {
      perror("Error opening file");
   if( fgets (str, 60, fp)!=NULL ) {
      /* writing content to stdout */


gets() is risky to use!, For reading a string value with spaces, we can use either gets() or fgets() in C programming language. Here, we will see what is the difference between gets()  A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

fgets() and gets() in C language, Let's see a simple program to read and write string using gets() and puts() functions. It suffers from buffer overflow, which can be avoided by using fgets(). Your former use of gets() consumed, but did not put the ' ' in its return. I long ago gave up doing user input with scanf() due to these subtle issues. Recommend to separate input from parsing, use fgets() and then sscanf() .

C gets() and puts(), This could be accomplished by an interagency committee or by a designated can immediately and permanently put in place the appropriate organizational  In order to get the best results from an alternative herbicide, it is best to use in conjunction with other helpful practices, like working to improve soil health, plant nutrition, and irrigation. Remember, an alternative is generally going to be a contact herbicide (except for electric shock), not a systemic herbicide, which kills the entire

Alternatives to Animal Use in Research, Testing, and Education: , Example: How gets() function works. #include <iostream> #include <cstdio> using namespace  The difference between gets() and fgets() is that gets() uses stdin stream. The gets() function provides no support to prevent buffer overflow if large input string are provided. Note: gets() was deprecated in C++11 and removed from C++14.

C++ gets() - C++ Standard Library, It all gets very tedious, but the point is that GOD is explaining all these rules. This is what he has to say – “Command the children of Israel, that they put out of the camp God could have provided an alternate method for the priests – such as  Start using these very high quality, non-Microsoft applications today on Windows and you can gradually decrease your dependence on Microsoft at your own pace. Every Microsoft alternative you use helps to keep choice and innovation in the software industry alive and it moves you one step closer to being free from everything that is wrong with

  • There is nothing wrong with using puts()
  • Sorry, why chose fputs() over puts()?
  • There are several other differences between gets() and fgets() including the most important one that the total number of input bytes is limited. The only big difference between puts() and fputs() is that fputs() can write to most any file while puts() can only write to stdout
  • @Sourav Ghosh: just for symmetry, since fgets takes a FILE stream as explicit parameter, the same could be used for the output side as well.
  • @user3629249: clear, there are many differences which led to remove gets from the standard. I meant a difference in usage...
  • error: too few arguments to function ‘fgets’ it is giving me this error message when i use fgets()
  • @BlytheS. at least read the manual. Nobody said it was a drop-in replacement.
  • @BlytheS. Please read the related man page, as linked in the answer.
  • @BlytheS. By asking a new question or search this site on how to use getchar()
  • @nos With all due respect sir, how to use getchar() would not be good fit for SO. In case there are issues during usage, we'd love to hear that.
  • Maybe you want one ungetc() when buffer is too short to avoid losing 1 character.
  • @pmg For mygets(), the idea is to always consume the entire line and not leave the extra part of the long line in stdin - returning an error condition.
  • My bad ... I read s = NULL; /*imagined*/break;, but you keep reading