2012-03-12 85 views
0

的我有以下几点:阅读将QString了一个文本文件/文本流错误

ReadValuesAndWriteIntoGUI() 
{ 
      QFile File (Directory + "/here/we/go"); 

      Qstring ValueAString 
      Qstring ValueBString 
      Qstring ValueCString 
      Qstring ValueDString 

     if(File.open(QIODevice::ReadOnly | QIODevice::Text)) 
      { 
      QTextStream Stream (&File); 
      QString Text; 
       do 
       { 
       Text = Stream.readLine(); 
       Text = Text.simplified(); 

    // Vala 
       QString startWith = "[1 -3 0 0 0 0 0]"; 
       QString endWith = ";" ; 
       int start = Text.indexOf(startWith, 0, Qt::CaseInsensitive); 
       int end = Text.indexOf(endWith, start + startWith.length(), Qt::CaseInsensitive); 
       if(start != -1) 
       ValueAString = Text.mid(start + startWith.length(), end - (start + startWith.length())); 
       qDebug() << ValueAString << (start + startWith.length()) << (end - (start + startWith.length())); 
       double Avalue = ValueAString.toDouble(); 
       ui.AInput->setValue(Avalue); 

    //Valb 
       QString startWith2 = "[1 -3 0 0 0 0 0]"; 
       QString endWith2 = ";" ; 
       int start2 = Text.indexOf(startWith2, start + startWith.length(), Qt::CaseInsensitive); 
       int end2 = Text.indexOf(endWith2, start2 + startWith2.length(), Qt::CaseInsensitive); 
       if(start2 != -1) 
       ValueBString = Text.mid(start2 + startWith2.length(), end2 - (start2 + startWith2.length())); 
       qDebug() << ValueBString << (start2 + startWith2.length()) << (end2 - (start2 + startWith2.length())); 
        double Bvalue = ValueBString.toDouble(); 
        ui.BInput->setValue(Bvalue); 

    //Valc 
        QString startWith3 = "[0 2 -1 0 0 0 0]"; 
        QString endWith3 = ";" ; 
        int start3 = Text.indexOf(startWith3, 0, Qt::CaseInsensitive); 
        int end3 = Text.indexOf(endWith3, start3 + startWith3.length(), Qt::CaseInsensitive); 
        if(start3 != -1) 
        ValueCString = Text.mid(start3 + startWith3.length(), end3 - (start3 + startWith3.length())); 
        qDebug() << ValueCString << (start3 + startWith3.length()) << (end3 - (start3 + startWith3.length())); 
         double CValue = ValueCString.toDouble(); 
         ui.CInput->setValue(CValue); 

    //Vald 
        QString startWith4 = "[0 2 -1 0 0 0 0]"; 
        QString endWith4 = ";" ; 
        int start4 = Text.indexOf(startWith4, start3 + startWith3.length(), Qt::CaseInsensitive); 
        int end4 = Text.indexOf(endWith4, start4 + startWith4.length(), Qt::CaseInsensitive); 
        if(start2 != -1) 
        ValueDString = Text.mid(start4 + startWith4.length(), end4 - (start4 + startWith4.length())); 
        qDebug() << ValueDString << (start4 + startWith4.length()) << (end4 - (start4 + startWith4.length())); 
        double Dvalue = ValueDString.toDouble(); 
        ui.DInput->setValue(Dvalue); 
        } 
       while(!Text.isNull()); 
       } 
} 

的文本文件是这样的:

File 
    { vers   0; 
    form   ci; 
    ass   dict; 
    loc  "cons"; 
    ect  Proper; 
    } 
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 


    a 
    { 
    Model New; 
    bla    bla [0 2 -1 0 0 0 0] 2;  //HERE I wanted to read ValueC=2 
    bli    bli [1 -3 0 0 0 0 0] 4;  //HERE I wanted to read ValueA=4 
    t   t [0 1 0 0 0 0 0] 0.003; 
} 


    b 
    { 
    Model New; 
    bla    bla [0 2 -1 0 0 0 0] 1;  //HERE I wanted to read ValueD=1 
    bli    bli [1 -3 0 0 0 0 0] 3;  //HERE I wanted to read ValueB=3 
    t   t [0 1 0 0 0 0 0] 0; 
} 
    . 
    . 
    . 

结果我在GUI中得到的是:

ui.AInput = 3 which originally is ValueB 
ui.BInput = 0 ? 
ui.CInput = 0,000000003000 ? 
ui.DInput = 0 ? 

正如您所看到的,我使用QString :: indexof和QString :: mid方法来获取QStrings,但到目前为止它不适用于这些e QStrings。有人知道我失败的地方吗?问候

回答

1

if (offset=0) =>if (offset==0);)

考虑到你的投入,我不明白你在想什么VALUEB应该的。您一次只读一行,因此总是只有一个startWith,因此只有ValueA。

这里是(我认为),你正在努力实现:

ReadValuesAndWriteIntoGUI() 
{ 

    QString ValueAString; 
    QString ValueBString; 

    QFile File (Directory + "/here/we/go"); 
    if(File.open(QIODevice::ReadOnly | QIODevice::Text)) 
    { 
     QTextStream Stream (&File); 
     QString Text; 
     int occ=0; 

     do 
     { 
      Text = Stream.readLine(); 
      Text = Text.simplified(); 

      QString startWith = "[1 -3 0 0 0 0 0]"; 
      QString endWith = ";" ; 

      int startWithPosition = Text.indexOf(startWith, 0, Qt::CaseInsensitive); 
      if(startWithPosition < 0) 
      { 
       break; 
      } 

      int endWithPosition = Text.indexOf(endWith, startWithPosition + startWith.length(), Qt::CaseInsensitive); 
      if(endWithPosition < 0) 
      { 
       break; 
      } 

      if (occ==0) 
      { 
       ValueAString = Text.mid(startWithPosition + startWith.length(), (endWithPosition - (startWithPosition + startWith.length()))); 
       qDebug() << ValueAString << (startWithPosition + startWith.length()) << (endWithPosition - (startWithPosition + startWith.length())); 
       double ValueA = ValueAString.toDouble(); 
       ui.ValueAInput->setValue(ValueA); 
       occ++; 
      } 
      else if (occ==1) 
      { 
       ValueBString = Text.mid(startWithPosition + startWith.length(), (endWithPosition - (startWithPosition + startWith.length()))); 
       qDebug() << ValueBString << (startWithPosition + startWith.length()) << (endWithPosition - (startWithPosition + startWith.length())); 
       double ValueB = ValueBString.toDouble(); 
       ui.ValueBInput->setValue(ValueB); 
      } 

     }       // close do command 
     while(!Text.isNull()); 

    }        //close method "ReadValuesAndWriteIntoGUI()" 
} 
+0

:)。奇怪的是,如果我这样做,ValueA获得ValueB的价值,而ValueB现在没有价值。当然,它是正确的,就像你写的那样:'if(offset == 0)',但似乎仍然存在错误。我编辑了我的问题! – Streight 2012-03-12 16:12:01

+0

你可以显示'Text',请 – Koying 2012-03-12 16:20:50

+0

现在我看看它,我猜QString endWith分别int endWithPostion可能是问题,因为endWith =“;”在文本:(经常出现。 – Streight 2012-03-12 16:20:57