C语言 C语言中的“标准输入”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19552360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:48:25  来源:igfitidea点击:

What means "standard input" in C language?

c

提问by Kio Marv

I was tasked with creating a test program in cthat reads the contents of the standard input and then prints them.

我的任务是创建一个测试程序,c该程序读取标准输入的内容,然后打印它们。

But I have a little doubt: what is exactly standard input?

但我有一点疑问:究竟是什么standard input

Is it what I type in the keyboard? Is it a file I have to read?

是我在键盘上输入的吗?这是我必须阅读的文件吗?

Both of them?

两个都?

Thanks.

谢谢。

回答by ratchet freak

it is what you type on the keyboard when you run the program from the command line

这是您从命令行运行程序时在键盘上键入的内容

it is one of the 3 standard streams defined for a program

它是为程序定义的 3 个标准流之一

when you start the program on a command line you can type some text i the terminal and that text will be passed to the standard input stream of the program

当您在命令行上启动程序时,您可以在终端中键入一些文本,该文本将被传递到程序的标准输入流

the 2 other streams are the standard out which is displayed on the terminal, and the error stream which is to display error messages that should not be in the standard out

其他 2 个流是显示在终端上的标准输出,以及显示不应该出现在标准输出中的错误消息的错误流

on most terminals you can redirect the streams to and from files like so:

在大多数终端上,您可以将流重定向到文件或从文件重定向,如下所示:

myprog.exe < file_to_read.txt 

where file_to_read.txt will be read and passed into the input input stream

其中 file_to_read.txt 将被读取并传递到输入输入流中

回答by John Bode

"Standard input" refers to a specific input stream, which is tied to file descriptor 0. It's the stream from which scanf, getchar, gets(which you should neveruse), etc., all read. Basically, any stdio input function that doesn't take a FILE *as an argument is reading from standard input.

“标准输入”指的是特定的输入流,它与文件描述符 0 相关联。它是从中读取scanfgetchargets(您永远不应该使用的)等的流。基本上,任何不将 aFILE *作为参数的stdio 输入函数都是从标准输入读取的。

It's usuallytied to your console, but can be redirected to read from a file or other device.

通常与您的控制台相关联,但可以重定向以从文件或其他设备读取。

For example,

例如,

scanf( "%d", &someVal );

is equivalent to

相当于

fscanf( stdin, "%d", &someval );

Both functions read from standard input (stdin).

这两个函数都从标准输入 ( stdin)读取。

回答by Peter K.

From Wikipedia:

来自维基百科:

Unless redirected, input is expected from the keyboard which started the program.

Unless redirected, input is expected from the keyboard which started the program.