Safely get string input from user including spaces

Stack Overflow 32

I know that I can use gets(char *) to get string input from user with spaces included but I read that buffer overflow problem. Using strcpy, strcmp (and a few other functions) is not safe we should use strncpy, strncmp and explicitly mention the size of the input. Some guy on Stack Overflow told me that. So, I am worried about using gets for getting input from user or it's safe? If it's safe tell me I'll continue using it. If not then what is the other way getting spaced string input from user safely?

#include <stdio.h> 
#include <stdlib.h> 

int main()
{
  char *a[2]; 

  char b[] = "first";
  char c[] = "second"; 

  int s1; //,s2;

 // printf("\nLong you want your %s string to be: \n",b);
 //scanf("%d",&s1); fflush(stdin);

 //printf("\nLong you want your %s string to be: \n",c);
 // scanf("%d",&s1); fflush(stdin);

  int i;
  for(i=0; i<2; i++) {
    printf("\nLong you want your %s string to be: \n",b);
    scanf("%d",&s1);  fflush(stdin); 
    a[i] = (char *) malloc(s1*sizeof(char)); 
  }

  printf("\nEnter the first string: \n");
  scanf("%s", a[0]); fflush(stdin);

  printf("\nEnter the second string: \n");
  scanf("%s", a[1]); fflush(stdin);

  printf("\nThe first string is: %s\n", a[0]);
  printf("\nThe second string is: %s\n", a[1]);
}
µtex

Please do some research.. this http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044652485&id=1043284385 may help you.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to echo a $_GET safely?

来自分类Dev

(int)$ _ GET ['user_input']安全吗?

来自分类Dev

Safely converting string to bool in PowerShell

来自分类Dev

How to get input text from soft keyboard

来自分类Dev

How to get type from String?

来自分类Dev

Get Facebook User ID from app-scoped User ID

来自分类Dev

How can i get from a FileInfo[] the file full name full directory including the file extension?

来自分类Dev

Validate comma delimited input without allow spaces

来自分类Dev

Unable to have methods from one class use user input from another class in Java

来自分类Dev

php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array

来自分类Dev

Haskell: trim String and eliminate multiple spaces

来自分类Dev

Including an AJAX-retrieved select input in an HTML form with ColdFusion

来自分类Dev

Including graph images in an email sent from Application

来自分类Dev

User input to repeat program in Java

来自分类Dev

Combining Regex and User Input in VBA

来自分类Dev

Xamarin ndk get string from c function as return value

来自分类Dev

How to get 8 random unique elements from a string array?

来自分类Dev

Is it possible to get String value back from its hash code?

来自分类Dev

How to randomly get a string from a collection but prefer strings at the beginning of the collection

来自分类Dev

How to get the parameter from a relative URL string in C#?

来自分类Dev

how to get array key from string array in java

来自分类Dev

Rails nested attributes are not creating an object from JSON string inside a hidden form input

来自分类Dev

Laravel请求input()或get()

来自分类Dev

Angularjs 和 Asp.net MVC - angular desn't get values from input type hidden

来自分类Dev

Get steamID by user nickname

来自分类Dev

Facebook PHP SDK how to get user email from Facebook JavaScript SDK 2014

来自分类Dev

In Lua, how to get all arguments including nil for variable number of arguments?

来自分类Dev

Laravel 5 $ request-> input与Input :: get

来自分类Dev

Without regex reduce multiple spaces in a string to one in Python

Related 相关文章

  1. 1

    How to echo a $_GET safely?

  2. 2

    (int)$ _ GET ['user_input']安全吗?

  3. 3

    Safely converting string to bool in PowerShell

  4. 4

    How to get input text from soft keyboard

  5. 5

    How to get type from String?

  6. 6

    Get Facebook User ID from app-scoped User ID

  7. 7

    How can i get from a FileInfo[] the file full name full directory including the file extension?

  8. 8

    Validate comma delimited input without allow spaces

  9. 9

    Unable to have methods from one class use user input from another class in Java

  10. 10

    php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array

  11. 11

    Haskell: trim String and eliminate multiple spaces

  12. 12

    Including an AJAX-retrieved select input in an HTML form with ColdFusion

  13. 13

    Including graph images in an email sent from Application

  14. 14

    User input to repeat program in Java

  15. 15

    Combining Regex and User Input in VBA

  16. 16

    Xamarin ndk get string from c function as return value

  17. 17

    How to get 8 random unique elements from a string array?

  18. 18

    Is it possible to get String value back from its hash code?

  19. 19

    How to randomly get a string from a collection but prefer strings at the beginning of the collection

  20. 20

    How to get the parameter from a relative URL string in C#?

  21. 21

    how to get array key from string array in java

  22. 22

    Rails nested attributes are not creating an object from JSON string inside a hidden form input

  23. 23

    Laravel请求input()或get()

  24. 24

    Angularjs 和 Asp.net MVC - angular desn't get values from input type hidden

  25. 25

    Get steamID by user nickname

  26. 26

    Facebook PHP SDK how to get user email from Facebook JavaScript SDK 2014

  27. 27

    In Lua, how to get all arguments including nil for variable number of arguments?

  28. 28

    Laravel 5 $ request-> input与Input :: get

  29. 29

    Without regex reduce multiple spaces in a string to one in Python

热门标签

归档