Monday, December 4, 2017

C++17(C11): The gets_s() Function

C++17(C11) removed the gets() function. The gets_s() function should be used instead. Here is an example:

#include <iostream>
#include <cstdio>
#include <cstdlib>


int main()
{
  char buffer[2] = {0, 0};

  try
  {
    gets_s(buffer, 2);
  }
  catch (...)
  {
  }
  std::cout << buffer << std::endl;

  return 0;
}
// Input : 5
// Output: 5
// Note: Any input longer that one character throws a program terminating 
//         exception on MSVS 2017.
References:
https://stackoverflow.com/questions/38405260/difference-between-c99-and-c11
http://en.cppreference.com/w/c/io/gets

No comments:

Post a Comment