C++:在两个程序中使用宽字符串时无法通过管道获取数据
我试图在 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);
但是,没有打印出任何数据。我怀疑问题在于外部程序输出到 wcout
和 wclog
,但我不知道如何处理它。我还尝试使用 wstring
和 fgetws
,但这也没有帮助。
我读到有关使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我正在覆盖外部程序用于输入的文件,因此它没有给出任何输出……
不过,将上述片段放在一个地方还是很好的,因为破译 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.