从管道中读取

发布于 2024-11-18 02:45:22 字数 473 浏览 2 评论 0原文

我需要从 C PIPE 读取总结果。我可以创建一个巨大的缓冲区并慢慢填充它,但这似乎效率低下并且非常消耗内存,所以我想知道是否有人有任何想法如何做到这一点。

这是我的代码:

FILE* pipe = popen(command, "r");
if (!pipe)
{
    strcpy("FAIL", output);
    return -1;
}

char buffer[128];

while (!feof(pipe))
{
    if (fgets(buffer, 128, pipe) != NULL)
    {
        // should add buffer to main one here
    }
}

任何帮助将不胜感激。

在 C++ 中,我可以将其添加到字符串中,但我不确定在 C 中如何执行此操作。

编辑:我不知道最终缓冲区的大小。

I need to read the total result from a C PIPE. I could just create a massive buffer and slowly fill it, but that seems inefficient and very memory hungry, so I'm wondering if anyone has any ideas how to do it.

Here is my code:

FILE* pipe = popen(command, "r");
if (!pipe)
{
    strcpy("FAIL", output);
    return -1;
}

char buffer[128];

while (!feof(pipe))
{
    if (fgets(buffer, 128, pipe) != NULL)
    {
        // should add buffer to main one here
    }
}

Any help would be greatly appreciated.

In C++ I could just add it to a String, but I'm not sure how I'd do that in C.

Edit : I don't know the size of the final buffer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-11-25 02:45:22

我可以创建一个巨大的缓冲区并慢慢填充它,但是那
看起来效率很低而且很记性
饿了

如果您需要将所有内容都记录在内存中 - 这是唯一的方法。如果需要很长时间才能将所有数据收集在一起,您可以将临时结果缓存在临时文件中,但最终您将不得不以某种方式填充巨大的内存缓冲区。临时缓存可以位于发送方或接收方,具体取决于您。

我不确定我能否解决您的困境...

编辑

经过一番澄清后,问题似乎在于提前不知道所需的内存量。

对于这个问题,realloc 确实是处理方法之一,使用文件作为临时缓存方法也可以解决这个问题,而不会造成 realloc 造成的额外内存性能损失(但相反,您会受到时间性能影响,因为磁盘 I/O 速度要慢得多)。

I could just create a massive buffer and slowly fill it, but that
seems inefficient and very memory
hungry

If you need everything to be in the memory - that's the only way to go. You can cache interim results in a temporary file, if it takes long time to gather all the data together, but eventually you'll have to have that huge memory buffer filled one way or another. The interim caching can be either at the sender side or the receiver, up to you.

I'm not sure I can follow your dilemma...

edit

After some clarification it appears that the problem is that the amount of the needed memory is not known ahead of time.

For that realloc is indeed one of the ways to handle, using a file as the interim caching method will take care of that as well without the additional memory performance hit that realloc causes (but instead you get a time performance hit because disk I/O is much slower).

庆幸我还是我 2024-11-25 02:45:22

您可以从相当小的缓冲区大小开始,然后在空间不足时将其加倍。这应该将缓冲区大小保持在合理的范围内,但不会进行大量的重新分配。 (我从评论中得知,您事先不知道必要的大小。)我想说 realloc 是一个相当安全的选择,使用此方法您只需调用它几次。

You could start with a fairly low buffer size and then double it whenever you run out of space. This should keep buffer size within a reasonable bound but won't take a huge number of reallocations. (I gathered from comments that you don't know the necessary size ahead of time.) I would say that realloc is a fairly safe bet and with this method you'll only call it a few times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文