簡體   English   中英

如何確定Popen流的大小?

[英]How to determine the size of popen stream?

我想確定由popen()函數調用恢復的流大小。 我嘗試使用fseek和ftell,但它返回的大小為-1。 誰能建議我如何確定文件大小? 以下是我正在使用的代碼。

   char return_val[256];
FILE *fp = NULL;
char line[256];
memset (return_val, 0, 256);
/* set the defalut value */
strncpy (return_val, "N/A", 4);
char cmd[] = "if [ -f /etc/version ]; then cut -d, -f1 -s /etc/version ; fi";

/* Open the command for reading. */
fp = popen(cmd, "r");
if (fp != NULL) 
{
    /* read the line from file */
    fgets (line, 256, fp);
    if( line != NULL)
    {
            /* copy the data */
            strncpy(return_val, line, strnlen (line, 256)); 
        }
      /* close the file */ 
    pclose (fp);
}

你不能 popen給您一個管道,一個FIFO先進先出流。 角色一方面進入,另一方面又出現。 您事先不知道管道的本質,因此將傳輸多少字節。

您可以求助於頻帶信令,要傳輸多少字節,也可以在自己的一側實現緩沖和動態分配。

在管道上也無法進行搜索,因為它只是這些過程之間的一種“門戶”。 就像在游戲“ Portal” /“ Portal2”中一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM