我使用以下代码从 QFile 中查找读取字节数。对于某些文件,它会给出正确的文件大小,但对于某些文件,它会给出一个近似为 fileCSV.size()/2 的值。我正在发送两个文件,其中的字符数相同,但文件大小不同link text。我应该使用其他一些对象来读取 QFile 吗?
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 的大小是错误的,如果可以的话,给我一些其他的解决方案。
谢谢你。