为什么得到()不工作? [英] why gets() is not working?

查看:125
本文介绍了为什么得到()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程用C在Unix中,
而且我在使用从键盘读取输入。
我总是得到这样的警告,程序停止运行:

I am programming in C in Unix, and I am using gets to read the inputs from keyboard. I always get this warning and the program stop running:

warning: this program uses gets(), which is unsafe.

谁能告诉我这是为什么发生的原因是什么?

Can anybody tell me the reason why this is happening?

推荐答案

作为在previous答案使用与fgets 而不是获得所提

As mentioned in the previous answers use fgets instead of gets.

但它不喜欢获得不工作的话,那就是非常非常不安全的。我的猜测是,你必须在你的code中的错误似乎与与fgets 以及因此请发表您的来源。

But it is not like gets doesn't work at all, it is just very very unsafe. My guess is that you have a bug in your code that would appear with fgets as well so please post your source.

修改
根据您在您的评论给了更新的信息,我有几个建议。

EDIT Based on the updated information you gave in your comment I have a few suggestions.


  • 我建议寻找在您的母语一个不错的C教程,谷歌是你的朋友在这里。作为一本书我建议 C程序设计语言

如果你有新的信息,这对他们修改到你原来的职位,尤其是如果它是code是一个好主意,它将使人们更容易理解你的意思。

If you have new information it is a good idea to edit them into your original post, especially if it is code, it will make it easier for people to understand what you mean.

您正试图读取一个字符串,基本字符数组,成单个字符,当然是会失败。你想要做的就是像下面这样。

You are trying to read a string, basically an array of characters, into a single character, that will of course fail. What you want to do is something like the following.

char username[256];
char password[256];
scanf("%s%s", username, password);

随意评论/编辑,我甚至在基本的C很生疏。

Feel free to comment/edit, I am very rusty even in basic C.

编辑2 作为jamesdlin警告,用法 scanf函数危险可获得

EDIT 2 As jamesdlin warned, usage of scanf is as dangerous as gets.

这篇关于为什么得到()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆