0
所以我打算接受通過執行以下命令的文本文件:重定向標準輸入 - Visual C
$ sort < list.txt
我明白列表的文本文件被輸入到我的排序程序,但實際上,我怎麼使用我的程序內的信息?我如何閱讀它?
我的代碼:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
char c;
while (c != EOF){
c = getchar();
printf("%c ",c);
}
return (0);
}
例子:
$ ./sort.c < input.txt
./sort.c: line 3: $'\r': command not found
./sort.c: line 4: syntax error near unexpected token `('
'/sort.c: line 4: `int main(int argc, char** argv) {
'#包括'和'從讀stdin'。 –
2014-10-01 00:39:10
重定向是發生在您的程序之外的事情。在你的程序啓動之前,shell會關心打開'list.txt'文件,並將它連接到你的程序的標準輸入。你的程序不需要做任何事情。你只會注意到,如果你敢問它,'isatty(STDIN_FILENO)'將返回0。 – 5gon12eder 2014-10-01 00:44:45
@ 5gon12eder哦,我敢肯定你已經釘住了它。我的評論告訴*如何*;你的評論解釋了爲什麼*,因此更具教育意義。它只是沒有發生在我身上 - 我已經知道如何重定向如此長時間工作,它就像一條魚不注意水:) – 2014-10-01 00:48:58