Results 1 to 9 of 9

Thread: cannot handle macintosh end-of-line

  1. #1
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default cannot handle macintosh end-of-line

    I don't know how to read a macintosh text file. The end of line is a character '13'. It's '10' on unixes, 10+13 on windows.

    Here is the code I use :

    Qt Code:
    1. QFile file(fileName);
    2. if (!file.open(QFile::ReadOnly | QFile::Text))
    3. {
    4. QMessageBox::warning(this, tr("Application"),
    5. tr("Cannot read file %1:\n%2.")
    6. .arg(fileName)
    7. .arg(file.errorString()));
    8. return;
    9. }
    10.  
    11. QTextStream in(&file);
    12.  
    13. QString line;
    14. line = in.readLine();
    15. while (!line.isNull())
    16. {
    17. doneWords.append(line);
    18. line = in.readLine();
    19. }
    To copy to clipboard, switch view to plain text mode 

    The readline() function reads the whole file if it uses 13 as a newline char.
    How can I set the endline char beeing '13' ?
    Thank you !

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Try this:
    //line = in.readLine();
    while (!in.atEnd()) {
    line = in.readLine();
    doneWords.append(line);
    }

  3. #3
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Thanks for your suggestion.

    Doesn't work either. The problem is that Qt doesn't know that it's a mac style end-of-line.

    I read a bit of Qt'sources and it seams that only windows and unix text handling is present.

    I hope i'm wrong, but I fear I'm not.
    I'll have to do this by hand, but don't know how.
    It's a mater of changing every char(13) to char(10) and then load the file with a QTextStream.

  4. #4
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Up!

    No more suggestion ?
    Strange that I find nothing about this.

    I must be missing something.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    If those files aren't too long, you could try QString::split().

  6. #6
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Sadly, they can be rather large : from 80mb up to 500mb.

    I'm starting doing it manually. If I find a better way using QTextStream, I'll be glad to update my code

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Then maybe std::getline() will do?

  8. The following user says thank you to jacek for this useful post:

    gaboo (27th September 2006)

  9. #8
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Quote Originally Posted by jacek View Post
    Then maybe std::getline() will do?
    I'll have a look at it, thanks.

  10. #9
    Join Date
    Sep 2006
    Location
    Lyon, France
    Posts
    6
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot handle macintosh end-of-line

    Answer from someone on qt-interest mailing list :

    Unfortunately, this is a known bug. But the Trolls do not seem to care that
    the Mac folk have lots of old text files to read. (The line ending on Mac OS
    X is now LF, not CR.)

    You will have to implement your own readLine, or fix Qt's.

    Of course, the _right_ way is to check for LF, CR, or LF/CR for every line.
    Making any assumptions based on the platform or first line is dangerous.
    (For instance, Visual Studio will create mixed line ending files when you
    edit a different line ending type than the default. Edited lines will have
    the default, but other lines retain the original.)

    This has been a problem for years that is so easily solved. I fail to
    understand why some developers still try to force everyone else to use their
    line ending choice. It is such a Microsoft attitude.

    Keith

Similar Threads

  1. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  2. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 18:49
  3. QTextBrowser with line numbers
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2006, 13:23
  4. MouseEvent on a line
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 25th March 2006, 08:52
  5. How to detect new line?
    By whoops.slo in forum Newbie
    Replies: 6
    Last Post: 6th January 2006, 17:02

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.