2017-03-07 4 views
0

私は初心者からC++とQtの両方です。qfile内のQfileを読む

新しいユーザーが追加されると、ユーザー構成ファイルを読み込んで変更する必要があります。

[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; 
    } 

ファイルに存在する([「ユーザー」]で)特定のユーザーを取得することができますどのように任意の提案ので、私は、そうでない場合は、別の文字列を作成し、そのフィールドを書き換えることができます。私はenter image description here出力画面を添付している

は、私はあなたが、直接コンテンツを自分で解析する必要を「INIファイル」を読んでいないためにQSettingsクラスを使用することができると思う

答えて

1

を撃ちました。

QSettings settings("/home/sidheshwar/Desktop/temp.txt", QSettings::IniFormat); 
// read the [db-Host] host value 
QString host = settings.value("db-Host/host").toString(); 
+0

ありがとうございます。 – zodango

関連する問題