Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何确定popen流的大小?_C_Linux_Popen - Fatal编程技术网

C 如何确定popen流的大小?

C 如何确定popen流的大小?,c,linux,popen,C,Linux,Popen,我想确定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 /

我想确定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为您提供了一个管道,一个先进先出流程。角色从一边进去,从另一边出来。管道的本质是,您不知道将传输多少字节

您可以求助于带内信令,知道将有多少字节,或者在您的一侧实现缓冲和动态分配


在管道上也不可能进行搜索,因为它只是这些进程之间的一种“门户”。你知道,就像在游戏“门户”/“Portal2”中一样。首先:使用ftell/fseek来确定文件的大小完全是个难题。使用特定于平台的机制,例如stat(2)。其次,ftell不“将大小返回为-1”。它返回-1以指示发生了错误。也就是说,您试图调用ftell的文件不是常规文件。@William:谢谢您的回复。我想我不能在这里使用stat(),因为popen()只返回指向流的指针。stat()需要命名文件来检查属性。是的,ftell()返回一个-1(错误),因为我试图访问不是常规文件的popen()流。是否有任何工作可以确定poopen()流的大小?您所说的“流大小”是什么意思?一条河里有多少水?您可以在底层文件描述符上使用fstat,但是如果您想获取大小,那么这样做是没有意义的,因为大小的概念对于管道来说没有意义。@William:谢谢您的回复。我明白你说的话了。谢谢你的评论。