使用fread在块中打印管道输出,但fread未读取最后一个块

问题描述

我试图用Windows_popen上执行命令,并仅使用fread()读取块的管道输出(我对另一种方法持开放态度,只要它读取8192个字节的块即可我之所以要分块阅读,是因为我计划使用此方法以8192个分块的形式通过TCP发送输出

这是我的尝试:

#include <stdio.h>

int main(void) {
    const int BUFLEN = 8192;
    char buf[BUFLEN + 1] = { 0 };
    FILE* fpipe;

    // Call Popen to execute command and read the pipe's output.
    // the file is exactly 229401 bytes.
    fpipe = _popen("type 229401.txt","r");

    // Store command output.
    while (fread(buf,1,BUFLEN,fpipe)) {
        printf("%s",buf);
        //send(socket,buf,strlen(buf),0);
    }
    printf("\n");

    // Close the pipe.
    _pclose(fpipe);

    return 0;
}

为什么这段代码不读取整个文件?以及如何正确读取/打印整个文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)