添加带有自定义标准错误的 fprintf() 语句后标准输出挂起

发布于 2024-12-27 23:54:38 字数 1666 浏览 7 评论 0原文

我有一个带有成员函数 extractData() 的 C++ 类 Archive。该函数调用realExtractData(),它是在单独的C 库中实现的。

我想向 extractData() 函数传递一对 FILE * 实例,通常是 stdoutstderr,但我还想提供自定义文件指针的选项:

class Archive {
    public:
        ...
        int extractData(string id, FILE *customOut, FILE *customErr);
        ...
};

int
Archive::extractData(string id, FILE *customOut, FILE *customErr)
{
    if (realExtractData(id.c_str(), customOut) != EXIT_SUCCESS) {
        fprintf(stderr, "something went wrong...\n");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

如果我按列出的方式调用上面的内容,则将数据输出到标准输出时不会出现延迟。所有提取的数据几乎立即发送到标准输出 (stdout):

FILE *outFp = stdout;
FILE *errFp = stderr;
Archive *archive = new Archive(inFilename);

if (archive->extractData(id, outFp, errFp) != EXIT_SUCCESS) {
    fprintf(errFp, "[error] - could not extract %s\n", archive->getInFnCStr());
    return EXIT_FAILURE;
}

如果我更改 extractData() 以便其 fprintf() 调用使用 < code>customErr:

int
Archive::extractData(string id, FILE *customOut, FILE *customErr)
{
    if (realExtractData(id.c_str(), customOut) != EXIT_SUCCESS) {
        fprintf(customErr, "something went wrong...\n");  /* <-- changed this line */
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

...然后当我运行二进制文件时,二进制文件似乎在处理输入和打印到标准输出时挂起。

如果我将 fprintf() 改回使用 stderr 而不是 customErr,一切都会再次正常工作,,数据立即刷新到标准输出(我的 customOut)。

这是缓冲问题吗?有办法解决这个问题吗?

I have a C++ class Archive with a member function extractData(). This function calls realExtractData(), which is implemented in a separate C library.

I want to pass the extractData() function a pair of FILE * instances that are usually stdout and stderr, but I want to provide the option of custom file pointers, as well:

class Archive {
    public:
        ...
        int extractData(string id, FILE *customOut, FILE *customErr);
        ...
};

int
Archive::extractData(string id, FILE *customOut, FILE *customErr)
{
    if (realExtractData(id.c_str(), customOut) != EXIT_SUCCESS) {
        fprintf(stderr, "something went wrong...\n");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

If I call the above as listed, there is no delay in outputting data to standard output. All extracted data get sent to standard output (stdout) almost immediately:

FILE *outFp = stdout;
FILE *errFp = stderr;
Archive *archive = new Archive(inFilename);

if (archive->extractData(id, outFp, errFp) != EXIT_SUCCESS) {
    fprintf(errFp, "[error] - could not extract %s\n", archive->getInFnCStr());
    return EXIT_FAILURE;
}

If I change extractData() so that its fprintf() call uses customErr:

int
Archive::extractData(string id, FILE *customOut, FILE *customErr)
{
    if (realExtractData(id.c_str(), customOut) != EXIT_SUCCESS) {
        fprintf(customErr, "something went wrong...\n");  /* <-- changed this line */
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

...then when I run the binary, the binary seems to hang in processing input and printing to standard output.

If I change fprintf() back to using stderr and not customErr, things once again work properly, i.e., data are flushed to standard output (my customOut) immediately.

Is this a buffering issue? Is there a way to fix this?

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

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

发布评论

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

评论(1

是你 2025-01-03 23:54:38

“stderr 而不是 customErr”

标准错误是未缓冲的,这意味着它几乎立即打印出来。除非您使用低级操作系统调用,否则其他输出流会被缓冲,这意味着除非您使用 endl、::flush 或其他内容进行缓冲区刷新,否则它们将需要更长的时间来打印。

如果您想要进行低级操作系统调用并且正在使用 unix,请查看以下内容:

http://www.annrich.com/cs590/notes/cs590_lecture_2.pdf

我还没有阅读全文,但扫描了一下看起来它似乎与 Stevens 的《Unix 高级编程》一书中有类似的信息,该书确实讨论了这一点。

"stderr and not customErr"

Standard error is un-buffered which means it prints out almost immediately. other output streams are buffered unless you're using low-level OS calls, which means they will take longer to print unless you do a buffer flush with something like an endl, ::flush, or whatever else.

If you want to go for the low-level OS calls and you're working with unix, check this out:

http://www.annrich.com/cs590/notes/cs590_lecture_2.pdf

I haven't read the whole thing, but on scanning it it looks as if it has similar info to the good Stevens Advanced Programming in Unix book which defitely talks through this.

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