我有一個充滿了這樣的線條之下.txt文件:QDateTime :: fromString不接受我的QString?
- 2011-03-03 03.33.13.222 4 2000信息化業務...等布拉布拉
- 2011-03-03 03.33。 13.333 4 2000信息化業務...等布拉布拉
- 2011-03-03 03.33.13.444 4 2000信息化業務...等布拉布拉
在我的代碼某些時候,我做了一些計算和求,其中我只提取每行開頭的日期。現在,當我在文件的開頭定位正確時,我只提取日期和時間(以毫秒爲單位)「ex:2011-03-03 03.33.13.444」並轉換爲QDateTime對象。
假設我的文件指針是在一定行, 用的readLine的開端放置正確,我讀我的日期時間文本行,並轉換爲QDateTime對象
QDateTime dt;
char lineBuff[1024];
qint64 lineLength;
lineLength=file.readLine(lineBuff, 24);
dt = QDateTime::fromString(QString(lineBuff),"yyyy-MM-dd HH.mm.ss.zzz");
這是apsolutely正確的。
但是,現在的問題是:
當我做同樣的是這樣的:
QDateTime dt;
QByteArray baLine;
char lineBuff[1024];
file.seek(nGotoPos); //QFile, nGotoPos = a position in my file
QString strPrev(baLine); // convert bytearry to qstring -> so i can use mid()
// calculate where the last two newline characters are in that string
int nEndLine = strPrev.lastIndexOf("\n");
int nStartLine = strPrev.lastIndexOf("\n", -2);
QString strMyWholeLineOfTextAtSomePoint = strPrev.mid(nStartLine,nEndLine);
QString strMyDateTime = strMyWholeLineOfTextAtSomePoint.left(24);
// strMyDateTime in debug mode shows me that it is filled with my string
// "ex: 2011-03-03 03.33.13.444"
// THE PROBLEM
// But when i try to covert that string to my QDateTime object it is empty
dt = QDateTime::fromString(strMyDateTime ,"yyyy-MM-dd HH.mm.ss.zzz");
dt.isValid() //false
dt.toString() // "" -> empty ????
,但如果我做的:
DT = QDateTime :: fromString(「2011-03 -03 03.33.13.444「,」yyyy-MM-dd HH.mm.ss.zzz「); 然後一切都很好。
什麼可能是我QString的問題? 我是否需要追加到strMyDateTime「\ 0」還是需要其他轉換?
我改變了這一點,沒有成功。與readLine有什麼不同,docs說它附加「\ 0」。我不認爲我需要爲QString做這件事。編輯:當我更改爲23我從右側獲得一個字符(缺少第三個組成部分) – PathOfNeo
然後,您可能在提取的字符串的開頭處有一個不可見的字符。我的猜測是LF或CR。 –
你說得對 - 我在開始的時候可能有一個空間,我扔了一個修剪(),現在是一切都很好。謝謝 – PathOfNeo