PDA

View Full Version : How to get number of bytes read from QTextStream



slobo_n
8th June 2010, 15:01
The following code I am using to find the number of read bytes from QFile. With some files it gives the correct file size, but with some files it gives me a value that is approximatively fileCSV.size()/2. I am sending two files that have same number of characters in it 4745, but have different file sizes. Should i use some other objects for reading the 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;
}

I know that reading the size of QString is wrong, give me some other solutions if you can.

Thank you.

numbat
9th June 2010, 11:34
You've been introduced into the wonderful world of character encodings. The same character can be represented in different ways and different numbers of bytes, depending on the encoding. Specifically, while both of your files have 16 characters, one is encoded with a single byte per character while the other is encoded with two bytes per character(probably UTF-16) and a two byte 'byte order mark'.

You should be aware that on some platforms a new line is represented by '\n' while on others it is "\r\n", so just adding two to each line may give incorrect results. Also remember the last line may not be terminated with a new line.

slobo_n
9th June 2010, 17:08
I made a solution with QByteArray. The solution is:

QFile fileCSV("someFile.txt");
if ( !fileCSV.open(QIODevice::ReadOnly | QIODevice::Text))
emit errorOccurredReadingCSV(this);
while ( !fileCSV.atEnd())
{
QByteArray arrayCSV = fileCSV.readLine();
reconstructedCSVFileSize += arrayCSV.size();
QTextStream textStreamCSV(arrayCSV);
QString line = textStreamCSV.readLine();
}

But there is a problem. Look close the files that I am sending 4754.

When i am reading biggerFile.csv with this approach, the first line is properly read, the size of the string is 108, also the number of characters is 108. The number returned by arrayCSV.size() is 221.
When i am reading the second line, the size of the string is 50, but the number of characters is 25. The number returned by arrayCSV.size() is 51. When i open the string with debuger, the string is empty, although its size is 50. I guess this behavior is because the first line is written with one encoding, while the other is written with different encoding, causing QTextStream to behave non properly.

When i am reading smallerFile.csv, everything is ok. The size of the string is 16, also the number of characters is 16(without the \n character). The number returned by arrayCSV.size() is 18.
The second line is also properly read. The size of the string is 25, also the number of characters is 25. The number returned by arrayCSV.size() is 25.

The first code that i have posted, reads the strings properly from both files.