【问题标题】:read Qfile in qt在qt中读取Qfile
【发布时间】:2017-03-07 11:09:41
【问题描述】:

我是 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;
    }

任何建议如何让特定用户(在 ["user"] 中)出现在文件中,以便我可以重写其字段,否则创建另一个字符串。

我已附上enter image description here输出截图

【问题讨论】:

    标签: c++ qt qt-creator qfile


    【解决方案1】:

    我想你可以使用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();
    

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 2013-05-20
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2018-07-15
      • 2017-01-21
      • 1970-01-01
      相关资源
      最近更新 更多