下面的代碼是用來從QFile中查找讀取字節數的。對於某些文件,它會提供正確的文件大小,但對於某些文件,它會給我一個近似fileCSV.size()/ 2的值。我發送兩個文件的字符數相同,但文件大小不同link text。我是否應該使用其他對象來閱讀QFile?如何獲取從QTextStream中讀取的字節數
QFile fileCSV("someFile.txt");
if (!fileCSV.open(QIODevice::ReadOnly | QIODevice::Text))
emit errorOccurredReadingCSV(this);
QTextStream textStreamCSV(&fileCSV); // use a text stream
int fileCSVSize = fileCSV.size());
qint64 reconstructedCSVFileSize = 0;
while (!textStreamCSV.atEnd())
{
QString line = textStreamCSV.readLine(); // line of text excluding '\n'
if (!line.isEmpty())
{
reconstructedCSVFileSize += line.size(); //this doesn't work always
reconstructedCSVFileSize += 2;
}
else
reconstructedCSVFileSize += 2;
}
我知道讀取QString的大小是錯誤的,如果可以的話,給我一些其他的解決方案。
謝謝。
我的代碼正在工作的一半;)。 我需要一些方法來讀取字節數。如果您知道其他方法來實現這一點,請回復。謝謝。 – user261882 2010-06-08 14:14:00
QFile :: size()有什麼問題?如果您想訪問文件中的原始字節,請使用QDataStream而不是QTextStream。 – Job 2010-06-08 14:20:16
QFile :: size()沒有錯,每次都會給出正確的文件大小。問題是我不知道如何獲取從文件讀取的字節數,我將逐行讀取文件。 我會閱讀關於QDataStream的想法... – user261882 2010-06-08 14:24:56