Results 1 to 3 of 3

Thread: How to get number of bytes read from QTextStream

  1. #1
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question How to get number of bytes read from QTextStream

    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 files..zip, but have different file sizes. Should i use some other objects for reading the QFile?

    Qt Code:
    1. QFile fileCSV("someFile.txt");
    2. if ( !fileCSV.open(QIODevice::ReadOnly | QIODevice::Text))
    3. emit errorOccurredReadingCSV(this);
    4. QTextStream textStreamCSV( &fileCSV ); // use a text stream
    5. int fileCSVSize = fileCSV.size());
    6. qint64 reconstructedCSVFileSize = 0;
    7. while ( !textStreamCSV.atEnd() )
    8. {
    9. QString line = textStreamCSV.readLine(); // line of text excluding '\n'
    10. if (!line.isEmpty())
    11. {
    12. reconstructedCSVFileSize += line.size(); //this doesn't work always
    13. reconstructedCSVFileSize += 2;
    14. }
    15. else
    16. reconstructedCSVFileSize += 2;
    17. }
    To copy to clipboard, switch view to plain text mode 

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

    Thank you.
    Last edited by slobo_n; 8th June 2010 at 15:44. Reason: updated contents

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get number of bytes read from QTextStream

    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.

  3. The following user says thank you to numbat for this useful post:

    slobo_n (9th June 2010)

  4. #3
    Join Date
    Dec 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get number of bytes read from QTextStream

    I made a solution with QByteArray. The solution is:
    Qt Code:
    1. QFile fileCSV("someFile.txt");
    2. if ( !fileCSV.open(QIODevice::ReadOnly | QIODevice::Text))
    3. emit errorOccurredReadingCSV(this);
    4. while ( !fileCSV.atEnd())
    5. {
    6. QByteArray arrayCSV = fileCSV.readLine();
    7. reconstructedCSVFileSize += arrayCSV.size();
    8. QTextStream textStreamCSV(arrayCSV);
    9. QString line = textStreamCSV.readLine();
    10. }
    To copy to clipboard, switch view to plain text mode 

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

    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.

Similar Threads

  1. How to read only a certain amount of bytes
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 08:38
  2. Read/Write from QTextStream
    By tonde in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2008, 17:51
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 5th July 2007, 00:12
  4. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 21:23
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.