在 C++ 中使用 C 库的标准输出应用
我有用 C 编写的代码,我正在努力创建一个库,我可以从中访问其他语言的函数。
我目前正在为此代码编写一个 C++ 客户端,以找出需要进行哪些更改才能为将我的库包含到 C++ 应用程序中提供支持。
通常,基于 C 的二进制文件会分析一些输入,然后通过一系列 fprintf
语句将其数据输出到 stdout
。
有没有办法将相关 C 函数的输出重定向到 stdout?
我不是 C++ 专家,但我认为我想做的是提供 C++ 输出流缓冲区的子类(ostream
和 ofstream
?),其中包含与相关 C 函数发送到 stdout
的输出相同。
如果可能的话,我希望可以选择将此数据重定向到文件流或 C++ 应用程序中的标准输出。
我该怎么做?我可以按原样保留 C 代码并从 C++ 代码中重定向 stdout
吗?
编辑
我可以编辑相关的 C 函数,以便文件指针或描述符是一个参数。能够传递输出的位置会有帮助吗?我不清楚如何翻译 C++ IO 和 C IO。我无法将 C++ iostream
传递给 C 函数,或者可以吗?
I have code written in C, which I'm working towards making a library from which I can access functions from other languages.
I am presently writing a C++ client to this code, to figure out what changes are necessary to provide support for including my library into a C++ application.
Normally, the C-based binary analyzes some input, and it then outputs its data to stdout
through a series of fprintf
statements.
Is there a way to redirecting output from the relevant C functions sent to stdout
?
I am not a C++ expert, but I think what I would like to do is provide a subclass of a C++ output stream buffer (ostream
and ofstream
?), which contains the same output that would otherwise get sent by the relevant C functions to stdout
.
I would like the option of redirecting this data to a file stream or to standard output within the C++ application, if possible.
How might I do this? Can I leave my C code as-is and redirect stdout
from within the C++ code?
EDIT
I can edit the C functions in question, so that a file pointer or descriptor is an argument. Would this help, being able to pass in where the output goes? What's unclear to me is how I would translate C++ IO and C IO. I can't pass in a C++ iostream
to a C function, or can I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在评论中,我提到 C++ std::cout 和 C stdout 以一种棘手的方式相关,并且您可以在 C++ 中使用 stdout通过包含
。我还提供了以下链接: http:// gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/manual/manual/io_and_c.html但是,您可能会考虑更改库的 API,并提供写入用户提供的流和/或用户提供的文件路径的函数。
在 Linux 上,您还可以使用 open_memstream 或 fopencookie
In a comment I mentioned that C++
std::cout
and Cstdout
are related in a tricky way, and that you can usestdout
in C++ by including<cstdio>
. I also gave the following link: http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/manual/manual/io_and_c.htmlHowever, you might consider changing the API of your library, and provide functions which write to a user supplied stream, and/or to a user supplied file path.
On Linux, you might also use open_memstream or fopencookie
如果我正确理解你的问题,不,至少对于符合标准的代码。
可能相关的是 std::ios_base::sync_with_stdio ,它确保某些 C++ 流上的 IO 与 C 流的 IO 正确同步。因此,stdout 与 cout 同步,stdin 与 cin 同步,stderr 宽度 cerr 同步。但这并不是为了允许更改 cout 中的streambuf并将更改传播到stdout(这就是我理解您的请求的方式),并且如果您在stdout上执行freopen,我还不能确定预期的效果。
If I've understood your question correctly, no, at least with standard conforming code.
What could be related is std::ios_base::sync_with_stdio which ensures that IO made on some C++ streams are correctly synchronized with IO make of C streams. So stdout is synchronized with cout, stdin with cin and stderr width cerr. But that isn't designed to allow changing the streambuf in cout and have the change propagated to stdout (which is how I understood your request), and I'm far from sure on the intented effect if you do a freopen on stdout.
您需要设置一个管道,并将C程序作为独立运行其标准输出连接到管道的输入端的单独进程。然后,您的 C++ 程序需要从管道的输出端读取,然后它将获取 C 进程的所有输出。
更新:在投票和评论之后,我又重新阅读了这个问题。我(现在)意识到 C 代码作为库运行,即链接到与 C++ 代码相同的可执行文件中。
我认为你不能在不影响 C++ 程序输出流的情况下强制重定向 C 代码的 stdout,这似乎是为了拥有一个库而付出的高昂代价。
我仍然认为解决方案是在自己的进程中对 C 代码进行“沙箱”处理,并提供重定向的输出,然后您可以根据需要读取该输出。当然,最好重写 C 功能以
返回
结果而不是打印结果,但我认为这不是一个选择。你说C代码使用
fprintf()
,即具有显式文件句柄的变体,所以也许你可以使用它并使其从外部接受FILE *
,在这种情况下,您可以为其提供一个管道,而无需对其进行子处理。很想看到其他答案,也许我缺少一种方法。
更新2:如果您有机会控制将哪个
FILE *
传递到C库中,那么很容易:stdout
即可。fopen()
该文件并传入生成的FILE *
。如果您想同时进行两者,那就又有点麻烦了。在这种情况下,我会再次推荐使用管道或套接字,以便 C++ 端负责读出 C 库生成的输出,并用它做它想做的事情。
You need to set up a pipe, and run the C program as a stand-alone process with its stdout connected to the input end of the pipe. Your C++ program then needs to read from the output end of the pipe, and it will then get all the output from the C process.
UPDATE: I re-read the question a bit more after the downvotes and comments. I (now) realize the C code is running as a library, i.e. linked into the same executable as the C++ code.
I don't think you can forcibly redirect stdout for the C code without affecting the C++'s program's output stream at the same time, which seems like a very high price to pay in order to have a library.
I still think the solution is to "sandbox" the C code in a process of its own, with a redirected output that you can then read as desired. It would of course be better to re-write the C functionality to
return
results instead of printing them, but I assume that's not an option.You say that the C code uses
fprintf()
, i.e. the variant with an explicit file handle, so perhaps you can use that and make it accept aFILE *
from the outside, in which case you can feed it a pipe without having to subprocess it.Very interested in seeing other answers, perhaps there's an approach I'm missing.
UPDATE 2: If you have the chance to control which
FILE *
you pass into the C library, then it's easy:stdout
.fopen()
the file and pass in the resultingFILE *
.If you want to do both at the same time, that's a bit more troublesome, again. In that case I would, again, recommend a pipe or socket so that the C++ side is responsible for reading out the output generated by the C library, and do what it wishes with it.