QTextStream的使用
2015-04-03 23:51
239 查看
#include <QCoreApplication> #include <QTextStream7gt; #include <QFile> #include <QDebug7gt; void write() { QFile file("C:/Test/simple.txt"); if(file.open(QIODevice::WriteOnly | QIODevice::Text)) { // We're going to streaming text to the file QTextStream stream(&file); stream << "Debussy\n"; stream << "Rabel\n"; file.close(); qDebug() << "Writing finished"; } } void read() { QFile file("C:/Test/simple.txt"); if(file.open(QIODevice::ReadOnly|QIODevice::Text)) { // We're going to streaming the file // to the QString QTextStream stream(&file); QString line; do { line = stream.readLine(); qDebug() << line; } while(!line.isNull()); file.close(); qDebug() << "Reading finished"; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); write(); read(); return a.exec(); }
Output:
Writing finished "Debussy" "Rabel" "" Reading finished
It's also common to use QTextStream to read console input and write console output. QTextStream is locale aware, and will automatically decode standard input using the correct codec. Example:
QTextStream stream(stdin); QString line; do { line = stream.readLine(); } while (!line.isNull());
相关文章推荐
- QTextStream的使用 读写文件
- 使用XmlTextWriter对象创建XML文件
- 在C++Builder使用TTS(Text To Speech)
- Adodb.Stream 组件的使用说明
- Adodb.Stream 组件的使用说明
- 使用Bitmap.FromStream(GetType().Assembly.GetManifestResourceStream())出现System.ArgumentException的解决办法
- 关于Adodb.Stream的使用说明
- VB.NET 使用DirectSound9 (3) Stream&Audio
- 存储过程中使用Text类型的输出参数(转)
- 尽量不要使用Class.class.getResourceAsStream
- 一段精简的使用 ADODB.Stream 读写"大字段"的 VB 代码!
- SmileyTextField 组件使用
- 关于Adodb.Stream 的使用说明
- 使用java.text包格式化数字和日期
- 关于Adodb.Stream 的使用说明
- 入门计划->使用(C++库)ofstream写文件数据
- 使用java.text包格式化数字和日期-ZT
- 使用 PEAR的Text_CAPTCHA保护Web表单[翻译]
- VB.NET 使用DirectSound9 (3) Stream&Audio
- WSE 2.0中使用Stream作为附件