C++:在两个程序中使用宽字符串时无法通过管道获取数据

发布于 2024-08-28 06:33:09 字数 1109 浏览 10 评论 0原文

我试图在 Mac OS X Snow Leopard 上使用以下 C++ 代码通过管道获取外部程序的输出。

FILE * al = popen("program program.cfg", "r");

string data;
char buffer[100];
while (fgets(buffer, 100, al) != NULL)
data.append(buffer);
cout << "«" << data << "»" << endl;

pclose(al);

但是,没有打印出任何数据。我怀疑问题在于外部程序输出到 wcoutwclog,但我不知道如何处理它。我还尝试使用 wstringfgetws,但这也没有帮助。

我读到有关使用 boost::iostreams 的内容,但再次没有运气:

FILE * al = popen("program program.cfg", "r");
boost::iostreams::file_descriptor_source alDesc(fileno(al));
boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> alStream(alDesc);
istream align(&alStream);

string alignOutput;
while (align) {
    getline(align, alignOutput);
    cout << "«" << alignOutput << "»" << endl;
}
align >> alignOutput;
alStream.close();
alDesc.close();

pclose(al);

有人知道实际问题可能是什么以及如何解决它吗?如果有人可能会问,外部程序和从管道读取的程序都需要使用 wstring 因为我正在处理可能是任何语言的数据,包括中文等。

提前感谢任何线索!

I’m trying to use the following code in C++ on Mac OS X Snow Leopard to get the output of an external program through a pipe.

FILE * al = popen("program program.cfg", "r");

string data;
char buffer[100];
while (fgets(buffer, 100, al) != NULL)
data.append(buffer);
cout << "«" << data << "»" << endl;

pclose(al);

However, no data gets printed out. I suspect the problem lies in the fact that the external program outputs to wcout and wclog, but I’m not sure how to deal with it. I also tried using a wstring and fgetws, but that didn’t help either.

I read about using boost::iostreams and again had no luck:

FILE * al = popen("program program.cfg", "r");
boost::iostreams::file_descriptor_source alDesc(fileno(al));
boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> alStream(alDesc);
istream align(&alStream);

string alignOutput;
while (align) {
    getline(align, alignOutput);
    cout << "«" << alignOutput << "»" << endl;
}
align >> alignOutput;
alStream.close();
alDesc.close();

pclose(al);

Does anyone have a clue as to what the actual problem might be and how to resolve it? In case someone might ask, both the external program and the one reading from the pipe need to use wstring as I’m dealing with data that might be in any language, including Chinese etc.

Thanks in advance for any clues!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-09-04 06:33:09

事实证明,我正在覆盖外部程序用于输入的文件,因此它没有给出任何输出……

不过,将上述片段放在一个地方还是很好的,因为破译 Boost 文档并不容易。

It turned out that I was overwriting a file the external program used for input, so it did not give any output…

Still, it’s nice to have the above snippets at one place, as it wasn’t straightforward to decipher the Boost documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文