在QT中读取Qfile
read Qfile in qt
我是C 和QT的初学者。
我必须读取用户配置文件并在添加新用户时更改它。
[db-Host]
type=1
host=10.25.36.15
charsize=0
foreground=0
background=0
Cursor=0
AnswerBack=None
CursorKeypad=0
termtype=vt220
capslock=0
CharacterSet=0
Num Lock=0
maxrows=0
maxcols=0
Cser_connec=0
functionkey_set=0
mouseCursor=1
autoStart=1
euro=0
我尝试了简单的CPP文件以如下读取。
#include "mainwindow.h"
#include <QApplication>
#include<QFile>
#include<QString>
#include <QTextStream>
QTextStream cout(stdout);
QTextStream cerr(stderr);
int main(int argc, char *argv[])
{
QString str, newstr;
QTextStream strbuf(&str);
QString useName = "[db-Host]";
QString pi = "ServerName=55333";
cout << "An in-memory stream" << endl;
strbuf << "userName: " << lucky << endl
<< "pi: " << pi << endl;
QFile data("/home/sidheshwar/Desktop/temp.txt");
cout << "Read data from the file - watch for errors." << endl;
if(data.open( QIODevice ::ReadOnly)) {
QTextStream in(&data);
QString lucky2;
in >> newstr >> lucky2>>endl;
if (lucky != lucky2)
cerr << "ERROR! wrong " << newstr << lucky2 << endl;
else
cout << newstr << " OK" << endl;
QString pi2;
in >> newstr >> pi2;
if (pi2 != pi)
cerr << "ERROR! Wrong " << newstr << pi2 << endl;
else
cout << newstr << " OK" << endl;
data.close();
}
return 0;
}
任何建议如何获得文件中存在的特定用户(在["用户")中,以便我可以重写其字段,否则创建另一个字符串。
i已连接的图像说明在此处输出屏幕截图
我认为您可以直接使用QSettings
类直接读取" INI文件",无需自己解析内容。
QSettings settings("/home/sidheshwar/Desktop/temp.txt", QSettings::IniFormat);
// read the [db-Host] host value
QString host = settings.value("db-Host/host").toString();
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- qfile qtextStream仅读取文件的一部分
- Qt - 为什么无法使用 QFile 读取文件,其中包含从 FileDialog 获取的目录
- 在QT中读取Qfile
- QFile:如何有效地读取从k到k+L的字节
- QT - QFile -从文件读取缓慢
- QFile不读取文件
- 从不同线程中的QFile读取
- 如何使用QSerialPort读取数据,使用QFile写入文件
- 如何从QFile中读取有符号的16位整数