![](/img/trans.png)
[英]How to use dup2 to redirect stdin and stdout to pipe file descriptors?
[英]Use of Stdin and Stdout
我必須使用示例代碼,其中必須提供文件源名稱和目標文件名稱 。 然后這些名稱將被發送到另一個函數,如下面的代碼所示
int def(FILE *source, FILE *dest, int level)
{
----
-----
return Z_OK;
}
int main(int argc, char **argv)
{
int ret;
/* avoid end-of-line conversions */
SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout);
// do compression if no arguments
if (argc == 1)
{
ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
{
zerr(ret);
return ret;
}
// otherwise, report usage
else
{
fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);
return 1;
}
}
return 0;
}
我不了解, stdin和stdout如何傳遞輸入和輸出文件名。 此外,我無法提供正確的源文件名和目標文件名順序作為命令參數。 大多數時候,我會zpipe usage: zpipe [-d] < source > dest
。
更新:完整的示例代碼位於此鏈接
使用stdin
和stdout
,不傳遞文件名。 調用main
時,這些文件已經打開,程序可以讀取/寫入它們。 然后,父進程(通常是外殼程序)負責將數據提供給stdin
並對來自程序stdout
的數據進行處理。
在您的示例用法zpipe < sourcefile > destinationfile
,外殼程序打開要讀取的源文件 ,並將打開的文件提供給zpipe ,還創建了要寫入的目標文件,並將程序輸出到其stdout
任何內容寫入其中。 zpipe程序不獲取任何一個文件的名稱,而僅獲取打開的文件句柄。
另外, < sourcefile
和> destinationfile
不是zpipe的參數。 Shell如上所述解析和處理它們, zpipe不會將它們用作參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.