QTextStream
2010-05-10 13:13
176 查看
QTextStream 像是一个适配器,能支持多数据源 #include <QTextStream > #include <QString > #include <QFile > QTextStream cout(stdout, QIODevice::WriteOnly); QTextStream cerr(stderr, QIODevice::WriteOnly); int main() { QString str, newstr; QTextStream strbuf(&str); //使用QString做数据源 int lucky = 7; float pi = 3.14; double e = 2.71; cout << "An in-memory stream" << endl; strbuf << "luckynumber: " << lucky << endl << "pi: " << pi << endl << "e: " << e << endl; cout << str; QFile data("mydata"); data.open(QIODevice::WriteOnly); QTextStream out(&data); //使用QDevice做数据源 out << str ; data.close(); cout << "Read data from the file - watch for errors." << endl; if(data.open(QIODevice::ReadOnly)) { /*Make sure the file exists before attempting to read.*/ QTextStream in(&data); int lucky2; in >> newstr >> lucky2; if (lucky != lucky2) cerr << "ERROR! wrong " << newstr << lucky2 << endl; else cout << newstr << " OK" << endl; float pi2; in >> newstr >> pi2; if (pi2 != pi) cerr << "ERROR! Wrong " << newstr << pi2 << endl; else cout << newstr << " OK" << endl; double e2; in >> newstr >> e2; if (e2 != e) cerr << "ERROR: Wrong " << newstr << e2 << endl; else cout << newstr << " OK" << endl; data.close(); } cout << "Read from file line-by-line" << endl; if(data.open(QIODevice::ReadOnly)) { QTextStream in(&data); while (not in.atEnd()) { newstr = in.readLine(); cout << newstr << endl; } data.close(); } return 0; } 评注:从上面的例子可以看出,QTextStream不负责文件的打开与创建,仅仅提供文件访问的方法,实现了功能的独立性 ,由于它支持多数据,不区分读写的方向,比fstream流使用起来更加方便
相关文章推荐
- QFile QBuffer QTextStream QDataStream用法
- QTextStream不输出换行
- QTextStream和QDataStream的区别
- QTextStream
- Expected mime type application/octet-stream but got text/html.
- stream:error (text) 错误分析
- solr异常--Expected mime type application/octet-stream but got text/html.
- QT输入输出(二) 之 QTextStream 测试
- QTextStream & QString 配合
- Qt qtextstream读取文件
- QTextStream的使用
- QTextStream读写文件
- QTextStream 读取文件内容
- VBS教程:方法-OpenAsTextStream 方法
- QTextStream 读取文件乱码的解决办法
- QTextStream读取坐标
- Solr4.6 Expected content type application/octet-stream but got text/html 解决办法
- QTextStream格式带中文对齐输出问题
- SparkStreaming socketTextStream
- QTextStream 居然接受FILE*这样的传统参数