如何在std :: iostream中使用QFile? [英] How to use a QFile with std::iostream?

查看:47
本文介绍了如何在std :: iostream中使用QFile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用像std :: iostream这样的QFile?我很确定那里一定有包装纸.问题在哪里?

我还有另一个库,它需要一个std :: istream作为输入参数,但是在我的程序中,此时我只有一个QFile.

解决方案

我想出了自己的解决方案(它使用了Stephen Chu提出的相同想法)

#include <iostream>
#include <fstream>
#include <cstdio>

#include <QtCore>

using namespace std;

void externalLibFunction(istream & input_stream) {
    copy(istream_iterator<string>(input_stream),
         istream_iterator<string>(),
         ostream_iterator<string>(cout, " "));
}

ifstream QFileToifstream(QFile & file) {
    Q_ASSERT(file.isReadable());        
    return ifstream(::_fdopen(file.handle(), "r"));
}

int main(int argc, char ** argv)
{
    QFile file("a file");
    file.open(QIODevice::WriteOnly);
    file.write(QString("some string").toLatin1());
    file.close();
    file.open(QIODevice::ReadOnly);
    std::ifstream ifs(QFileToifstream(file));
    externalLibFunction(ifs);
}

输出:

some string

此代码使用27.9.1.7 basic_ifstream构造函数部分中指定的std :: ifstream移动构造函数(C ++ x0功能). org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf"rel =" nofollow noreferrer> 工作草案,编程语言C ++标准 :

basic_ifstream(basic_ifstream&& rhs);
效果:将构造从 右值这是通过完成 构造基类,并 包含的basic_filebuf.下一个 basic_istream :: set_rdbuf(& sb)被调用以安装包含的 basic_filebuf.

有关此主题的讨论,请参见如何返回fstream(C ++ 0x).

Is it possible to use a QFile like a std::iostream? I'm quite sure there must be a wrapper out there. The question is where?

I have another libs, which requires a std::istream as input parameter, but in my program i only have a QFile at this point.

解决方案

I came up with my own solution (which uses the same idea Stephen Chu suggested)

#include <iostream>
#include <fstream>
#include <cstdio>

#include <QtCore>

using namespace std;

void externalLibFunction(istream & input_stream) {
    copy(istream_iterator<string>(input_stream),
         istream_iterator<string>(),
         ostream_iterator<string>(cout, " "));
}

ifstream QFileToifstream(QFile & file) {
    Q_ASSERT(file.isReadable());        
    return ifstream(::_fdopen(file.handle(), "r"));
}

int main(int argc, char ** argv)
{
    QFile file("a file");
    file.open(QIODevice::WriteOnly);
    file.write(QString("some string").toLatin1());
    file.close();
    file.open(QIODevice::ReadOnly);
    std::ifstream ifs(QFileToifstream(file));
    externalLibFunction(ifs);
}

Output:

some string

This code uses std::ifstream move constructor (C++x0 feature) specified in 27.9.1.7 basic_ifstream constructors section of Working Draft, Standard for Programming Language C++:

basic_ifstream(basic_ifstream&& rhs);
Effects: Move constructs from the rvalue rhs. This is accomplished by move constructing the base class, and the contained basic_filebuf. Next basic_istream::set_rdbuf(&sb) is called to install the contained basic_filebuf.

See How to return an fstream (C++0x) for discussion on this subject.

这篇关于如何在std :: iostream中使用QFile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆