Results 1 to 3 of 3

Thread: Read unicode text from file

  1. #1
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Read unicode text from file

    Hello,

    I have a text file that is encoded in some "Unicode format". I do not know which Unicode this might be (in fact I don't know anything about Unicode at all, to be honest).
    When I open this file in my editor (the old Crimson Editor under WinXP) I have to manually set the encoding type to "Unicode Encoding". Only then I can read the file content.

    What I really want to do is to read the file content in a Qt based application.
    The following code prints only one cryptic line. The line looks like *â– 2.
    Qt Code:
    1. QFile logFile("C:\\cl.txt");
    2. logFile.open( QIODevice::ReadOnly );
    3. QString line = logFile.readLine();
    4. while( !logFile.atEnd() ) {
    5. qout << line; // qout is a QTextStream working on stdout
    6. line = logFile.readLine();
    7. }
    To copy to clipboard, switch view to plain text mode 
    The file does not contain special characters. The whole file content could easily be treated as pure ASCII text. But it is in some Unicode encoding.
    How can I read the file content?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read unicode text from file


  3. #3
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Read unicode text from file

    Thank you, the setAutoDetectUnicode(true) seemed to be the solution.
    For those who should ever have a comparable problem, the following code did it:
    Qt Code:
    1. FILE* fp = fopen( logFile.fileName().toAscii(), "r" );
    2. QTextStream in(fp, QIODevice::ReadOnly);
    3. in.setAutoDetectUnicode(true);
    4.  
    5. QString line;
    6. do
    7. {
    8. if( line.contains(QString("[REPORT]")))
    9. {
    10. qout << line << endl;
    11. }
    12. line = in.readLine();
    13. } while (!line.isNull());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to read the text of a pdf file?
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 22nd January 2016, 10:45
  2. Read from text file
    By kulsekarr in forum Newbie
    Replies: 3
    Last Post: 8th June 2012, 12:11
  3. can't read from unicode file .
    By gbmtoday in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2010, 14:36
  4. Read Text file using structure..
    By umulingu in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2009, 11:22
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47

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.