开发者

How to redirect the stdout and stderr and preserve the order?

I'm writing a simple code editor for the LC3 assembly language, and I've run into a small problem. LC3 assembler outputs basic information about the status of assembling which I'm trying to display in my program after reading it back from the log file.

Originally I attempted to use system() to call the LC3 assembler and redirect the stdout and stderr to a file, like so:

system("/pathto/lc3as 1> lc3.log 2>&1");

It开发者_开发技巧 outputs to the file but doesn't preserve the order of the output from the assembler.

I have come up with an ugly work around that preserves the order of the output but involves a lot more file IO than I'd like, and I'm not sure if it would work in every situation. On that note, I'm wondering if there is a better solution for routing the output to my program that preserves the order.


Once you redirect output, the output stream switches from "flush after every write" to "flush only when buffer full" mode. Your stderr output is now going be completely out of sync with the stdout output. You'd have to explicitly call fflush() yourself. Maybe your CRT has a function to change the mode.

Also consider not fixing this. Nobody ever redirects progress chatter to a file, only stderr output matters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜