Results 1 to 16 of 16

Thread: Problem with reading from file

  1. #1
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Problem with reading from file

    Hi,

    I am trying to read from a file using readLine function. This is my code.

    QFile file("tempfile");
    if (!file.open(QIODevice::ReadOnly ))
    {
    return;
    }
    while (!file.atEnd())
    {

    QString line = file.readLine();
    qWarning(line.toAscii());
    }

    But some characters in the read line is not printing like " ' " as it is . For example if the actual file content is this

    str_cpy.c: In function ‘str_cpy’:

    It is read and printed as

    In function ‘str_cpy’:

    What can I do to print these lines like the file content itself. What am I missing here.

    Jayanth.S

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with reading from file

    have a look at QTextStream, using it you can set needed encoding.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    Thanks spirit for the quick reply. I tried QTextStream::setCodec() function to change to different character coding schemes. But none of them is working. I think characters like " ' " is causing the problem. Which is the encoding scheme that supports these characters.

    Jayanth.S

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with reading from file

    Quote Originally Posted by iamjayanth View Post
    I think characters like " ' " is causing the problem. Which is the encoding scheme that supports these characters
    Everyone! What encoding has your file. Make sure you use the same. Under Linux you can type under console
    Qt Code:
    1. $ file yourfile.txt
    To copy to clipboard, switch view to plain text mode 
    to get the encoding. And please show us, how you have used QTextStream.

  5. #5
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    QFile file("tempfile");
    if (!file.open(QIODevice::ReadOnly ))
    {
    return;
    }
    QTextStream out(stdout);
    out.setCodec("UTF-8");
    while (!file.atEnd())
    {
    QString line = file.readLine();
    line.remove(0,len);
    if(line.contains("error",Qt::CaseSensitive)!=0)
    {
    out<<line;
    list->addItem(line.toAscii());
    }
    else if(line.contains("warning",Qt::CaseSensitive)!=0)
    {
    out<<line;
    list->addItem(line.toAscii().data());
    }
    else
    {
    out<<line;
    list->addItem(line.toAscii().data());
    }
    }

    This is how I used QText stream to read from a file ' tempfile ' and write the content to stdout and also to a list widget (list) . I tried file command in console for tempfile

    $ file tempfile
    tempfile: UTF-8 Unicode text

    That is the reason I chose UTF-8 as codec for QTextStream. How to fix that problem.

  6. #6
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    QFile file("tempfile");
    if (!file.open(QIODevice::ReadOnly ))
    {
    return;
    }
    QTextStream out(stdout);
    out.setCodec("UTF-8");
    while (!file.atEnd())
    {
    QString line = file.readLine();
    line.remove(0,len);
    if(line.contains("error",Qt::CaseSensitive)!=0)
    {
    out<<line;
    list->addItem(line.toAscii());
    }
    else if(line.contains("warning",Qt::CaseSensitive)!=0)
    {
    out<<line;
    list->addItem(line.toAscii().data());
    }
    else
    {
    out<<line;
    list->addItem(line.toAscii().data());
    }
    }

    This is how I used QText stream to read from a file ' tempfile ' and write the content to stdout and also to a list widget (list) . I tried file command in console for tempfile

    $ file tempfile
    tempfile: UTF-8 Unicode text

    That is the reason I chose UTF-8 as codec for QTextStream. Still when I print the content of the file into stdout and listwidget some characters are not printing (" ' " for example).Thanks for the reply

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with reading from file

    Use QTextStream for reading the file not only for giving the wrong read text out!

    Edit: From the QFile documentation
    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4.  
    5. QTextStream in(&file);
    6. while (!in.atEnd()) {
    7. QString line = in.readLine();
    8. process_line(line);
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    Hi,

    Now I am able to print to stdout correctly. But when I try to insert the line into QListWidget problem persists. Below given my code.

    QFile file("tempfile");
    if (!file.open(QIODevice::ReadOnly ))
    {
    return;
    }
    QTextStream in(&file);
    QTextStream out(stdout);
    in.setCodec("UTF-8");
    while (!in.atEnd())
    {
    QString line = in.readLine();
    out<<line<<'\n';
    list->addItem(line.toAscii().data());
    }

    When I add the line to listwidget I am still getting problems. This time " ' " is printing as " ? ".

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with reading from file

    Qt internally uses UTF-16.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #10
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    Then how can I display the contents read from file in the listwidget as it is .

  11. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with reading from file

    try this
    Qt Code:
    1. QByteArray encodedString = "...";
    2. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    3. QString string = codec->toUnicode(encodedString);
    To copy to clipboard, switch view to plain text mode 
    read more about QTextCodec.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #12
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    I think I have corrected the problem.... I used setUtf16() function to convert UTF-8 to UTF-16. I dont know if this is the correct way. But it worked.

    Qt Code:
    1. list->addItem(line.setUtf16(0,line.length()));
    To copy to clipboard, switch view to plain text mode 

    Thanks spirit and lykurg for your valuable time. I am now reading QTextCodec to check for any better ways... Thanks again.

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with reading from file

    while (!in.atEnd())
    {
    QString line = in.readLine();
    out<<line<<'\n';
    list->addItem(line.toAscii().data());
    }
    Please use the [ CODE ] tags and simply write:
    Qt Code:
    1. while (!in.atEnd())
    2. {
    3. QString line = in.readLine();
    4. out<<line<<'\n';
    5. list->addItem(line); // <--
    6. }
    To copy to clipboard, switch view to plain text mode 
    There is no need to transform the QString. And by using toAscii() - as you could expect - it is destroying the UTF-8 by converting to ascii...

  14. #14
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    hi,

    So lykurg, you are saying the encoding was UTF-8 from the start. I just need to pass it onto listwidget. I think i have done the same earlear but didnt worked . But now it is working. The only difference is that I used normal file reading methods in the first time and QTextStream now. What is the difference in using QTextStream.

  15. #15
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with reading from file

    Quote Originally Posted by iamjayanth View Post
    So lykurg, you are saying the encoding was UTF-8 from the start. I just need to pass it onto listwidget. I think i have done the same earlear but didnt worked .
    No you haven't. You have used the readLine directly on the QFile. And how the docs says: This function reads a line of ASCII characters from the device... So you tried to read UTF8 with a ASCII reader which leads to garbage in your QString. And setting garbage via your QString to the table widget won't heal the content.

    But now it is working. The only difference is that I used normal file reading methods in the first time and QTextStream now. What is the difference in using QTextStream.
    In QTextStream there is the difference. QTextStream respects the encoding of a file and do perform conversions on the bytes which it reads. So you get the right data in your QString.

  16. #16
    Join Date
    Oct 2008
    Location
    Thodupuzha, a small town in kerala
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Problem with reading from file

    Thanks Lykurg....That cleared it all....Thank you very much....

Similar Threads

  1. EXE File problem
    By triperzz in forum Installation and Deployment
    Replies: 8
    Last Post: 18th January 2008, 19:00
  2. Debug problem in Eclipse with qrc file
    By Vortex in forum General Programming
    Replies: 6
    Last Post: 27th December 2007, 00:56
  3. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  4. QHttp "PUT METHOD" QT Problem File Upload. RFC 2616
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2006, 22:02
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 08:08

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.