添加带有自定义标准错误的 fprintf() 语句后标准输出挂起
我有一个带有成员函数 extractData()
的 C++ 类 Archive
。该函数调用realExtractData(),它是在单独的C 库中实现的。
我想向 extractData()
函数传递一对 FILE *
实例,通常是 stdout
和 stderr
,但我还想提供自定义文件指针的选项:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“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.