C语言 什么是“标准输入”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19540425/
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
What is “standard input”?
提问by Kio Marv
I was tasked with creating a test program in Cthat reads the contents of the standard inputand then prints them.
我的任务是创建一个测试程序,C该程序读取标准输入的内容,然后打印它们。
But I have a little doubt: what is exactly standard input?
但我有一点疑问:究竟什么是标准输入?
Is it what I type in the keyboard? Is it a file I have to read?
是我在键盘上输入的吗?这是我必须阅读的文件吗?
Both of them?
两个都?
And the same goes for standard output: is it the console? a file?
这同样适用于standard output:它是控制台吗?一份文件?
回答by Basile Starynkevitch
The C standard (e.g. C99 or C11) defines what should be expected from the standard <stdio.h>header (after having suitably #include-d it). See stdio(3)man page.
Then you have the stdinand stdoutand stderrfile handles (pointers to some FILEwhich is an abstract data type).
C 标准(例如 C99 或 C11)定义了标准<stdio.h>头文件的预期内容(在适当的#include-d 之后)。请参见stdio(3)手册页。然后你有stdinandstdout和stderr文件句柄(指向一些FILE抽象数据类型的指针)。
The fact that stdinis related to some device (e.g. a keyboard) is implementation specific.
stdin与某些设备(例如键盘)相关的事实是特定于实现的。
You could (but that would be unethical and/or inefficient) implement the C standard with e.g. a room of human slaves (that is unethical, if you use paid workers that would be just inefficient), instead of using a computer. Often, computers gives your some implementation of the C standard thru the help of some operating system.
您可以(但那将是不道德和/或低效的)使用例如人类奴隶的房间(这是不道德的,如果您使用效率低下的带薪工人)来实现 C 标准,而不是使用计算机。通常,计算机通过某些操作系统的帮助为您提供 C 标准的一些实现。
You may want to know, inside your C program, if stdinis a "keyboard" or redirected from some "file". Unfortunately, AFAIK, there is no C99-standard way to know that.
您可能想知道,在 C 程序中,stdin是“键盘”还是从某个“文件”重定向。不幸的是,AFAIK,没有 C99 标准的方法来知道这一点。
As you mention, stdin, stdoutand stderrshould be available in your program at startup (i.e. after entering main....). Hence, unless you fclosethe stdinstream, you can read it (with getchar, scanf, getline, fgets, fscanf... and friends) without any prior care (so you don't need to fopenit yourself).
正如您所提到的,stdin,stdout并且stderr应该在您的程序启动时可用(即在输入main.... 之后)。因此,除非你fclose是stdin流,否则你可以在没有任何事先关心的情况下阅读它(使用getchar, scanf, getline, fgets, fscanf... 和朋友)(所以你不需要fopen自己阅读)。
On Linux or most Posix systems, you might use as an approximation isatty(STDIN_FILENO)- see isatty(3)for more - to test if stdin"is" the "keyboard" (by testing if it is some tty). See also this& that.
在 Linux 或大多数 Posix 系统上,您可能会使用近似值isatty(STDIN_FILENO)——更多信息请参见isatty(3)——来测试stdin“是”“键盘”(通过测试它是否是某个 tty)。另见这个和那个。
回答by Kio Marv
Yes, standard input (stdin) is input exepected from the keyboard. So, could be in the form of user input from a basic program or from a command line argument. Standard output (stdout) is the output of the code, usually to the terminal window. You could output your code almost anywhere, i.e. to a file, to a textbox, browser, but the standard is the stdout which is the terminal.
是的,标准输入 (stdin) 是期望从键盘输入的。因此,可以采用来自基本程序或命令行参数的用户输入的形式。标准输出 (stdout) 是代码的输出,通常到终端窗口。您几乎可以在任何地方输出您的代码,即输出到文件、文本框、浏览器,但标准是标准输出,它是终端。
Hope that helps.
希望有帮助。
回答by PMF
Normally, the standard input is the keyboard and the standard output the screen. However, you can redirect this in the command line using the "<" and ">" symbols. A command line like
通常,标准输入是键盘,标准输出是屏幕。但是,您可以使用“<”和“>”符号在命令行中重定向它。像这样的命令行
dir /s > "Tree.txt"
will change the standard output for the dir command to be the specified file. So all output goes to that file. The called application or command itself doesn't normally even notice the difference.
会将 dir 命令的标准输出更改为指定的文件。所以所有输出都转到该文件。被调用的应用程序或命令本身通常甚至不会注意到差异。
回答by Farouq Jouti
standard output(or stdout) refers to the standardized streams of data that are produced by command line programs (i.e., all-text mode programs) in Linux and other Unix-like operating systems.
标准输出(或stdout)是指由 Linux 和其他类 Unix 操作系统中的命令行程序(即全文本模式程序)产生的标准化数据流。
回答by technosaurus
stdin is file descriptor 0, you can get a file to stdin by:
stdin 是文件描述符 0,您可以通过以下方式将文件获取到 stdin:
cat file |yourprog
#or
yourprog <file
likewise for stdout (file descriptor 1)
同样对于标准输出(文件描述符 1)
yourprog | someotherprog #pipe your stdout to the stdin of another program
yourprog > somefile #save stdout to a file
yourprog >> somefile #append stdout to a file
and stderr (fd 2)
和标准错误(fd 2)
yourprog 2> errlogfile
if you have a program that takes a file but doesn't handle stdin, you can use the above formats by doing this (assuming -f if the input file argument)
myprog -f /dev/stdin
如果你有一个接受文件但不处理标准输入的程序,你可以通过这样做来使用上述格式(假设 -f 如果输入文件参数)
myprog -f /dev/stdin
//and a horrible example of how not to read from stdin and write to stdout
char buf[4096];
while(write(1,buf,read(0,buf,4096)));

