为什么我不能使用C#中的Process获取C ++程序的输出?(Why can't I get the output of a C++ program using Process in C#

编程入门 行业动态 更新时间:2024-10-28 11:23:50
为什么我不能使用C#中的Process获取C ++程序的输出?(Why can't I get the output of a C++ program using Process in C#?)

我有一个C ++程序,它使用wprintf_s函数将结果打印到命令行。 但是当我在C#中使用Process来读取程序的输出时,我无法得到它的任何说法。 但是当我在wprintf_s语句之后添加了一个fflush(stdout) ,我终于可以在我的C#程序中读取标准输出了。

我用来启动进度的代码是:

var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "FILENAME", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); StringCollection values = new StringCollection(); proc.OutputDataReceived += (s, args) => { lock (values) { values.Add(args.Data); } }; proc.BeginOutputReadLine(); proc.WaitForExit();

谁能告诉我为什么fflush(stdout)会起作用?

I've got a C++ program which uses wprintf_s function to print the results into the command line. But when I uses Process in C# to read the output of the program, I cannot get any words of it. However when I added a fflush(stdout) after the wprintf_s statement, I can finally read the standard output in my C# program.

The code I use to start the progress is:

var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "FILENAME", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); StringCollection values = new StringCollection(); proc.OutputDataReceived += (s, args) => { lock (values) { values.Add(args.Data); } }; proc.BeginOutputReadLine(); proc.WaitForExit();

Can anybody tell me why a fflush(stdout) would work?

最满意答案

输出在C ++进程中被缓冲,并且将保持不变,直到缓冲区已满或刷新为止,例如通过调用fflush() ,关闭流或其他依赖于操作系统的原因。

fflush()只会导致输出缓冲区中的任何数据写入流。

如果您不想显式调用fflush() ,可以考虑通过使用第二个参数的NULL指针调用setbuf()来在输出流上设置无缓冲模式:

#include <stdio.h> #include <unistd.h> int main() { setbuf(stdout, (char *)NULL); while (1) { fputs("hi there\n", stdout); sleep(1); } }

现在输出将立即出现。

请注意,如果stdout是终端,则不需要setbuf(f, NULL) ,因为这是终端设备的默认行为。 如果stdout是一个管道,那么setbuf(f, NULL)将使它无缓冲。

The output is being buffered in the C++ process and will remain so until the buffer is full or it is flushed, e.g. by calling fflush(), by closing the stream, or other OS dependant reasons.

fflush() just causes any data in the output buffer to be written to the stream.

If you don't want to explicitly call fflush(), you can consider setting unbuffered mode on the output stream by calling setbuf() with a NULL pointer for the second argument:

#include <stdio.h> #include <unistd.h> int main() { setbuf(stdout, (char *)NULL); while (1) { fputs("hi there\n", stdout); sleep(1); } }

Now the output will appear immediately.

Note that if stdout is a terminal, setbuf(f, NULL) is unnecessary as this is the default behaviour for terminal devices. If stdout is a pipe, then setbuf(f, NULL) will make it unbuffered.

更多推荐

本文发布于:2023-08-04 21:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1421429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   程序   program   Process   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!