Qt入门-文件读写
来源:互联网 发布:foobar2000源码输出 编辑:程序博客网 时间:2024/05/17 23:15
二进制文件的读写文件可以使用QFile类、QStream
文本文件的读写建议使用QTextStream类,它操作文件更加方便。
打开文件时,需要参数指定打开文件的模式:
ConstantValueDescriptionQIODevice::NotOpen0x0000The device is not open.QIODevice::ReadOnly0x0001The device is open for reading.QIODevice::WriteOnly0x0002The device is open for writing.QIODevice::ReadWriteReadOnly | WriteOnlyThe device is open for reading and writing.QIODevice::Append0x0004The device is opened in append mode, so that all data is written to the end of the file.QIODevice::Truncate0x0008If possible, the device is truncated before it is opened. All earlier contents of the device are lost.QIODevice::Text0x0010When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.QIODevice::Unbuffered0x0020Any buffer in the device is bypassed.
QIODevice::Text在读写文本文件时使用,这样可以自动转化换行符为本地换行符。
(1)写入文本文件
QFile f("c:\\test.txt");if(!f.open(QIODevice::WriteOnly | QIODevice::Text)){cout << "Open failed." << endl;return -1;}QTextStream txtOutput(&f);QString s1("123");quint32 n1(123);txtOutput << s1 << endl;txtOutput << n1 << endl;f.close();
写入的文件内容为:
123
123
(2)读取文本文件
QFile f("c:\\test.txt");if(!f.open(QIODevice::ReadOnly | QIODevice::Text)){cout << "Open failed." << endl;return -1;}QTextStream txtInput(&f);QString lineStr;while(!txtInput.atEnd()){lineStr = txtInput.readLine();cout << lineStr << endl;}f.close();
屏蔽打印的内容为:
123
123
- Qt入门-文件读写
- Qt入门-文件读写
- Qt入门-文件读写
- Qt入门-文件读写
- Qt入门-文件读写
- QT5入门之20 -QT读写ini文件
- Qt|Qt读写ini文件
- QT 文件读写操作
- Qt 文件读写操作
- Qt读写ini文件
- Qt 读写XML文件
- QT 读写Xml文件
- QT 文件读写操作
- Qt读写ini文件
- QT读写ini文件
- Qt txt文件读写
- Qt读写ini文件
- QT 文件读写操作
- UIScrollView UITableView 上拉刷新源码
- Java多线程之sleep 和yield方法
- 如何在windows下编译Chrome源代码
- java环境变量配置
- 第一章 WEB简介内容 第二章 HTML,JavaScript简介
- Qt入门-文件读写
- 程序员面试100题之六:最长公共子串
- L2交换原理
- vim保存文件”:wq"与“:x"的区别
- Javascript基础笔记
- Swing中显示图片
- C++函数返回引用
- Install Adobe Air on 64-bit Ubuntu 12.04 Precise Pangolin
- C#模板类