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

标签 c++ c stdout stderr buffering

我有一个 C++ 类 Archive 和一个成员函数 extractData()。此函数调用在单独的 C 库中实现的 realExtractData()

我想向 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() 调用使用 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)。

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

最佳答案

“stderr 而不是 customErr”

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

如果您想进行低级操作系统调用并且您使用的是 unix,请查看:

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

我还没有读完整本书,但在浏览它时,它看起来好像与优秀的 Stevens Advanced Programming in Unix 书中有类似的信息,这本书肯定会讨论这个问题。

关于c++ - 添加带有自定义标准错误的 fprintf() 语句后标准输出挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8927117/

相关文章:

c++ - 枚举的无符号字符

c - 在c中调用free时发生双重释放或损坏错误

java - 编译静态链接的 netty-tcnative 失败,与来自 JDK 的 jni.h 不匹配

关闭 telnet session 会将 stdout 重定向到 http 套接字

c++ - 如何将类对象数组转换为 vector 数组?

c++ - 回调与 lambda

c++ - 共享指针在循环中超出范围时出错(堆损坏?)

c - 为什么 valgrind 报告未初始化值错误?

java - 将 System.out.println 或 stdout 数据捕获到 swing 备忘录(大数据)

python 子进程正在覆盖用于 stdout 的文件 - 我需要它附加到文件 (windows)