Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I read in a text file line by line that uses Hex0D as a line terminator?
Forum Updated to NodeBB v4.3 + New Features

How do I read in a text file line by line that uses Hex0D as a line terminator?

Scheduled Pinned Locked Moved General and Desktop
30 Posts 6 Posters 14.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlicemirrorA Offline
    AlicemirrorA Offline
    Alicemirror
    wrote on last edited by
    #5

    Jonathan,

    it's a solution similar to one I already adopted in past (that obviously I don't find now to post here...)

    Only an advice: take care of that "forever" loop. If you file is only one line (or only one character) without any \r character your loop never exit. Simply add in the forever loop a check: if file.read(1) doesn't returns a character, this means that you have reached the EOF so exit anyway from the forever loop.

    Enrico Miglino (aka Alicemirror)
    Balearic Dynamics
    Islas Baleares, Ibiza (Spain)
    www.balearicdynamics.com

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jonathan
      wrote on last edited by
      #6

      Using
      @ if (readChar == "\r") @
      works provided that I also drop QIODevice::Text, ie use
      @file.open(QIODevice::ReadOnly)@

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ivan.todorovich
        wrote on last edited by
        #7

        [quote author="Alicemirror" date="1299684492"]Jonathan,

        it's a solution similar to one I already adopted in past (that obviously I don't find now to post here...)

        Only an advice: take care of that "forever" loop. If you file is only one line (or only one character) without any \r character your loop never exit. Simply add in the forever loop a check: if file.read(1) doesn't returns a character, this means that you have reached the EOF so exit anyway from the forever loop.[/quote]

        You're right, instead of
        @forever@
        should be
        @while (!file.atEnd())@

        I editted the first post.

        o_o Sorry for my rusted english.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ivan.todorovich
          wrote on last edited by
          #8

          [quote author="Jonathan" date="1299684717"]Using
          @ if (readChar == "\r") @
          works provided that I also drop QIODevice::Text, ie use
          @file.open(QIODevice::ReadOnly)@[/quote]

          Indeed, and reading the documentation you'll see that QIODevice::Text was the root of your problem :)

          o_o Sorry for my rusted english.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on last edited by
            #9

            Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • AlicemirrorA Offline
              AlicemirrorA Offline
              Alicemirror
              wrote on last edited by
              #10

              Franzk, with the solution suggested by Ivan it does no matter.
              What you do is to read from the file one character at a time until you reach the end. For every character, you check if the value corresponds to what you consider the EOL, then the program close the text line and go ahead.
              Indeed, using this method your EOL character can be everything you want: is the program that create lines based on the character that you decide. When the line is complete, I suppose that the source creating a line string set automatically the EOL with the right character. A similar approach is those used to manage linux-encoded text files with windows programs and vice-versa.

              Enrico Miglino (aka Alicemirror)
              Balearic Dynamics
              Islas Baleares, Ibiza (Spain)
              www.balearicdynamics.com

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #11

                [quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
                Why should it? No platform supported by Qt uses that control character.

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

                1 Reply Last reply
                0
                • AlicemirrorA Offline
                  AlicemirrorA Offline
                  Alicemirror
                  wrote on last edited by
                  #12

                  Why complicating a simple thing? There are tons of cases where a file is non standard. Reading character by character the problem does not exist.

                  Or not ?

                  Enrico Miglino (aka Alicemirror)
                  Balearic Dynamics
                  Islas Baleares, Ibiza (Spain)
                  www.balearicdynamics.com

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #13

                    It would be nice to be able to control it though. If you have to produce files that have to be processed by other tools, control is needed.

                    1 Reply Last reply
                    0
                    • AlicemirrorA Offline
                      AlicemirrorA Offline
                      Alicemirror
                      wrote on last edited by
                      #14

                      Andre, what does you means with "control"? To set a control like those explained above or what ?

                      Enrico Miglino (aka Alicemirror)
                      Balearic Dynamics
                      Islas Baleares, Ibiza (Spain)
                      www.balearicdynamics.com

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #15

                        [quote author="Alicemirror" date="1299694794"]Andre, what does you means with "control"? To set a control like those explained above or what ?[/quote]

                        I mean that I, as a programmer, can choose what QTextStream and QIODevice considder the line separator. Sure, you can output your data character by character, but I did not choose Qt to do everything by hand...

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          ivan.todorovich
                          wrote on last edited by
                          #16

                          [quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]

                          Well, yeah.. That's the actual root of the problem :/

                          I've been reading too much Qt Labs Blog posts.. Sadly, it seems I'm becoming one of those qt-is-perfect-no-need-to-change-anything guys >.<

                          Jonathan, you should file a bug report http://bugreports.qt.nokia.com/

                          o_o Sorry for my rusted english.

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            ivan.todorovich
                            wrote on last edited by
                            #17

                            [quote author="Andre" date="1299694989"][quote author="Alicemirror" date="1299694794"]Andre, what does you means with "control"? To set a control like those explained above or what ?[/quote]

                            I mean that I, as a programmer, can choose what QTextStream and QIODevice considder the line separator. Sure, you can output your data character by character, but I did not choose Qt to do everything by hand...[/quote]

                            I agree with you. The best solution is indeed that.
                            QTextStream and QIODevice already have the subroutines to read a file up to some hardcoded characters. It would be wise to add some "control" on what these classes consider a EOL.

                            Then, the same routine could be used to read any character-separated file.

                            o_o Sorry for my rusted english.

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Franzk
                              wrote on last edited by
                              #18

                              [quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
                              Why should it? No platform supported by Qt uses that control character. [/quote]

                              Mac is no longer supported?

                              Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.

                              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • I Offline
                                I Offline
                                ivan.todorovich
                                wrote on last edited by
                                #19

                                [quote author="Franzk" date="1299695807"][quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
                                Why should it? No platform supported by Qt uses that control character. [/quote]

                                Mac is no longer supported?

                                Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.[/quote]

                                Just to add to your arguement, a quote from wikipedia:

                                http://en.wikipedia.org/wiki/Newline :
                                "Systems based on ASCII or a compatible character set use either LF (Line feed, '\n', 0x0A, 10 in decimal) or CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, '\r\n', 0x0D 0x0A). "

                                o_o Sorry for my rusted english.

                                1 Reply Last reply
                                0
                                • AlicemirrorA Offline
                                  AlicemirrorA Offline
                                  Alicemirror
                                  wrote on last edited by
                                  #20

                                  AH :)

                                  Andre, you are right! As a matter of fact I focused the attention of all my answers thinking that there was a non standard line terminated file, while - silly, real! - \r is the old, famous carriage return...

                                  Enrico Miglino (aka Alicemirror)
                                  Balearic Dynamics
                                  Islas Baleares, Ibiza (Spain)
                                  www.balearicdynamics.com

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    Jonathan
                                    wrote on last edited by
                                    #21

                                    As suggested, I've filed bug report QTBUG-18038.

                                    1 Reply Last reply
                                    0
                                    • D Offline
                                      D Offline
                                      dangelog
                                      wrote on last edited by
                                      #22

                                      [quote author="Franzk" date="1299695807"][quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
                                      Why should it? No platform supported by Qt uses that control character. [/quote]

                                      Mac is no longer supported?

                                      Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.[/quote]

                                      Therefore I'm allowed to argue that ASCII 0x07 (BEL) is a valid line ending in my wonderful system, therefore QTextStream should support it? :)

                                      Come on, stick to reality: if you need custom line endings handle the line splitting yourself. It's easy and always works.

                                      (BTW: where do those files come from? Mac OS 9?)

                                      Eventually, you can suggest an API extension to allow for custom line endings in QTextStream, and/or provide the implementation yourself (quite easy) and submit a merge request.

                                      Software Engineer
                                      KDAB (UK) Ltd., a KDAB Group company

                                      1 Reply Last reply
                                      0
                                      • J Offline
                                        J Offline
                                        Jonathan
                                        wrote on last edited by
                                        #23

                                        My code would have been much tidier if I could have specified the end of line character.

                                        But to be general, it would have to be a string. Didn't some systems use "x0Ax0D"?

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          dangelog
                                          wrote on last edited by
                                          #24

                                          [quote author="Franzk" date="1299695807"][quote author="peppe" date="1299694316"][quote author="Franzk" date="1299693469"]Actually, the root of the problem is that Qt doesn't accept \r as a valid EOL.[/quote]
                                          Why should it? No platform supported by Qt uses that control character. [/quote]

                                          Mac is no longer supported?

                                          Even if it is true, \r is still a valid line ending and should therefore be supported, even if only by configuration.[/quote]

                                          Mac OS 9 was never supported during Qt 3 nor Qt 4. Mac OS X uses Unix newlines ('\n').

                                          Software Engineer
                                          KDAB (UK) Ltd., a KDAB Group company

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved