Qt 文件读写
2015-08-26 13:58
513 查看
读
A:
QString
qss;
B:
QFile
listfile("D:/onlinelist.txt");
写
QFile f("D:/onlinelist.txt");
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
return -1;
}
QTextStream txtOutput(&f);
QString s1("123");
quint32 n1(123);
txtOutput << s1 << endl;
txtOutput << n1 << endl;
f.close();
二进制文件的读写文件可以使用QFile类、QStream
文本文件的读写建议使用QTextStream类,它操作文件更加方便。打开文件时,需要参数指定打开文件的模式:
Constant Value Description
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When 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::Unbuffered 0x0020 Any buffer in the device is bypassed.
QIODevice::Text在读写文本文件时使用,这样可以自动转化换行符为本地换行符。
A:
QString
qss;
QFile qssFile(":/bar.qss");
qssFile.open(QFile::ReadOnly);
if(qssFile.isOpen())
{
qss = QLatin1String(qssFile.readAll());
this->setStyleSheet(qss);
qssFile.close();
}
B:
QFile
listfile("D:/onlinelist.txt");
if(!listfile.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug()<<"Open failed.";
}
else
{
QTextStream textInput(&listfile);
QString lineStr;
while(!textInput.atEnd())
{
lineStr = textInput.readLine();
}
}
写
QFile f("D:/onlinelist.txt");
if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"Open failed.";
return -1;
}
QTextStream txtOutput(&f);
QString s1("123");
quint32 n1(123);
txtOutput << s1 << endl;
txtOutput << n1 << endl;
f.close();
二进制文件的读写文件可以使用QFile类、QStream
文本文件的读写建议使用QTextStream类,它操作文件更加方便。打开文件时,需要参数指定打开文件的模式:
Constant Value Description
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When 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::Unbuffered 0x0020 Any buffer in the device is bypassed.
QIODevice::Text在读写文本文件时使用,这样可以自动转化换行符为本地换行符。
相关文章推荐
- (一)Qt:键盘输入,两行editline
- vs2013 编译 x64 qt4.8.6
- Qt国际化(源码含中文时)的点滴分析
- 关于qt中的tr()函数
- Qt中使用ActiveX(二)【方法调用和事件处理】
- Qt入门学习——Qt快速入门(vim纯代码编写)
- QT (QML)for IOS 设置ios设备的 StatusBarStyle
- Qt 中显示中文
- QT的信号与槽机制介绍
- MFC和Qt优缺点
- qt优点
- 使用Qt(mingw)编译FreeType为 .a 静态库
- Qt 利用QSettings + config.ini 保存设置
- QT与Visual Studio 2010整合的例子
- Qt 学习之路 2(15):标准对话框 QMessageBox
- 关于QT 中 QDialog的几点,exec show(ZZ)
- Qt QpenGL 入门教程
- QT模态对话框
- opencv + Qt + codeblocks
- QT 对话框数据传递