【发布时间】:2014-09-07 14:33:21
【问题描述】:
我有一个可以调用的 qt-app:
cat bla.bin | myapp
在 Win、Mac 和 Linux 上将整个输入 (stdin) 读入 QByteArray 的最简单方法是什么?
我厌倦了几件事,但它们似乎都不起作用(在 Windows 上):
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QByteArray content;
//---Test 1: hangs forever, reads 0
while(!std::cin.eof()) {
char arr[1024];
int s = std::cin.readsome(arr,sizeof(arr));
content.append(arr,s);
}
//---Test 2: Runs into timeout
QFile in;
if(!in.open(stdin,QFile::ReadOnly|QFile::Unbuffered)) {
qDebug() << in.errorString();
}
while (in.waitForReadyRead(1000)) {
content+=in.readAll();
}
in.close();
return app.exec();
}
我是否遇到了事件循环问题,或者没有它就不能工作?
【问题讨论】:
-
Am I having a Event-Loop Problem?可能。因为事件循环在app.exec();之后开始。尝试读取直到文件关闭或在调用exec后移动读取 -
不要那样使用
QFile。QFile永远不会报告“已读”。std::cin的方法应该适用于任何地方,你是在 Windows 下使用CONFIG+=console编译的吗? -
根据文档我不需要事件循环。是的,我的 .pro 文件中有 CONFIG+=console 和 CONFIG-=app_bundle。它也不适用于 linux。
标签: c++ windows qt cross-platform