简体   繁体   English

C样式printf / scanf

[英]C style printf/scanf

I've seen, here and elsewhere, many questions that, to get input data, use something like this: 我在这里和其他地方都看到过很多问题,要获取输入数据,请使用类似以下的内容:

 ...
 printf("What's your name? ");
 scanf("%s",name);
 ...

This is very reminiscent of the old BASIC days ( INPUT for those who remember it). 这使人想起了BASIC的旧时代(对于记住它的人来说是INPUT )。

The majority of those questions, if not all, are from people just learning C and are homeworks or example taken from their book. 这些问题中的大部分(如果不是全部的话)都来自刚刚学习C的人,并且是家庭作业或从书中摘录的示例。

I clearly remember that when I learned CI was told that this type of question/answer style was not a good practice for getting user input. 我清楚地记得,当我得知CI时,这种类型的问题/答案风格并不是获取用户输入的好习惯。 The "Right Way" was either to get parameters on the command line ( argv[...] ) or reading from a data file to be parsed with fgets() . “正确的方法”是在命令行( argv[...] )上获取参数,还是从要使用fgets()进行解析的数据文件中读取。 When user friendliness was a must, termio and friends had to be used. 当用户友好性是必须的, termio朋友不得不使用。

Now, I wonder if anything changed in the past years. 现在,我想知道过去几年是否有任何变化。 Are people thaught to structure user interaction as a set question/answer now? 人们现在是否想将用户交互结构化为一个固定的问题/答案?

I can only see disadvantages in using the printf()/scanf() approach, the main one being the diversity of terminals (^H anyone?) that could make difficult for the user to correct mistakes. 我只能看到使用printf()/ scanf()方法的缺点,主要的缺点是终端的多样性(^ H有人吗?)可能会使用户难以改正错误。

Could anyone point me to concrete advantages of this approach? 谁能指出我这种方法的具体优势?

This structure is easy to explain and easy to learn, which is why it appears in so many introductory materials. 这种结构易于解释且易于学习,这就是为什么它出现在许多入门材料中的原因。 Doing user input "the right way" in C can appear fairly daunting to a neophyte, especially when you have to deal with tokenizing and conversions. 在C语言中用“正确的方式”进行用户输入对于新手来说似乎相当艰巨,尤其是当您必须处理标记化和转换时。

However, I agree that it would be valuable for introductory materials to demonstrate more robust methods for handling user input. 但是,我同意对于入门资料来说,展示更强大的方法来处理用户输入将是有价值的。

I always thought the Unix way was to accept input from stdin. 我一直以为Unix方式就是接受来自stdin的输入。 That way the caller of the command can pipe input in from another command, from a file or manually. 这样,命令的调用者可以从另一个命令,文件或手动管道输入。

fgets() / sscanf() or similar is the right way to accept user input. fgets() / sscanf()或类似方法是接受用户输入的正确方法。

Take what you read here and elsewhere with a (large) pinch of salt. 用一小撮盐把在这里和其他地方读到的东西。

The GNU readline library is really an excellent resource for this. GNU readline库确实是一个很好的资源。 Its main advantage is that it handles all the intricacies of editing, as well as letting users have their own input settings, eg Vi or Emacs mode. 它的主要优点是它可以处理所有复杂的编辑操作,并让用户拥有自己的输入设置,例如Vi或Emacs模式。

This is the library bash and many other programs that accept interactive line-based data use. 这是库bash和许多其他程序,它们接受基于行的交互式数据使用。 By using the library, you get an interface that your users will have some knowledge of how to use, plus you get all sorts of nice features without having to explicitly code support for line editing. 通过使用该库,您将获得一个界面,使您的用户将对如何使用它有所了解,此外,您还可以获得各种不错的功能,而不必显式地编写对行编辑的支持。

In many cases you are probably missing the point of the exercise if you think this is important. 如果您认为这很重要,在很多情况下您可能会错过本练习的重点。 There's a world of a difference between a homework exercise and real world programs. 家庭作业和实际程序之间存在很大的差异。 The purpose of such exercises is seldom, if ever, to teach user interface design; 这种练习的目的很少(如果有的话)来教用户界面设计。 the technique you describe is generally just a simple way to obtain test input for the real exercise, and is also probably encouraged by tutors who, pressed for time require submitted code to adhere to some 'course-style' to make marking easier. 您描述的技术通常只是获得实际练习的测试输入的一种简单方法,也可能受到辅导老师的鼓舞,他们迫切需要时间,要求提交的代码遵循某种“课程风格”,以便于打标。 'Course-style' is seldom the same thing as 'good-practice' or 'practical style', but that is not to say that it does not serve some purpose, or even that that purpose is beneficial to the students education. “课程风格”与“良好实践”或“实践风格”很少是同一回事,但这并不是说它没有任何目的,甚至那个目的也无益于学生的学习。

Unfortunately novices often get hung up on this stuff when it is generally irrelevant to the actual purpose of the exercise. 不幸的是,当新手通常与练习的实际目的无关时,他们经常会挂在它上面。 The problem is that user input using standard library primitives is not as foolproof and some tutors seem to think. 问题在于,使用标准库原语的用户输入并不十分安全,有些辅导者似乎认为。

I agree with CTFord, that IF you're doing a command line program, then stdin is a perfectly reasonable way to deal with input. 我同意CTFord的观点,即如果您正在执行命令行程序,则stdin是处理输入的完美合理方法。

However, now adays, the correct way is to make a window that has a text box a label and a button and blah blah blah. 但是,如今,正确的方法是使具有文本框的窗口带有标签和按钮,等等。

I know that doesn't really answer your question, but I think my point is that that style of programming has become MUCH less prevalent, so that it doesn't really matter what is "correct". 我知道这并不能真正回答您的问题,但我想我的意思是,这种编程风格已变得不那么普遍了,因此“正确”的内容并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM