开发者

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

开发者 https://www.devze.com 2022-12-16 01:42 出处:网络
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

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

精彩评论

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