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. Read a file and distinguish CR/LF and LF char

Read a file and distinguish CR/LF and LF char

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 7.2k Views
  • 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.
  • V Offline
    V Offline
    Vinorcola
    wrote on 8 Jan 2014, 14:21 last edited by
    #1

    Hello everybody

    Happy new year all of you!

    I come here because I need some help about file reading. Actually, I have a program that read a file through QTextStream. But now, I have to distinguish CR/LF and LF new line character. In fact, I want to know if each line in the file ended with CR/LF or with LF, knowing that the file have some line that finished by CR/LF and some other that finish by LF.

    Thanks a lot for your help.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      butterface
      wrote on 8 Jan 2014, 15:14 last edited by
      #2

      Use QTextStream::readLine and you will get every line which ends with CR/LF or LF (you will get it without them!)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vinorcola
        wrote on 8 Jan 2014, 15:40 last edited by
        #3

        But it is what I want: to get the "\n" or the "\r\n" into the read string. And QTextStream do not return the "end of line" character.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          butterface
          wrote on 8 Jan 2014, 15:54 last edited by
          #4

          QTextStream::readAll does contain all the whitespaces. If you want their position you might use QString::indexOf

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vinorcola
            wrote on 8 Jan 2014, 16:09 last edited by
            #5

            The problem is I'm working with huge files (sometime more than 200 MB of text). So I can't use readAll() because it load all the file content in memory.

            No, I'd like to read the file line by line. Maybe I have to subclass QFile and create my own reading method using readLineData()... But I wanted to be sure there is not an already existing solution for this problem ;)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              butterface
              wrote on 8 Jan 2014, 18:53 last edited by
              #6

              And why don't use readLine and just append LF or CR/LF?

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vinorcola
                wrote on 8 Jan 2014, 19:38 last edited by
                #7

                Because it is exactly what I want to know: In the file, there is some line ended by CR/LF and some other ended by LF (which I internaly do not treat like a real end of line, but as a new line in the data itself). Well it is a bit tricky.

                But to summarize, the file containts some lines with CR/LF and some lines with LF. And I want to know when I'm reading the file if the line I'm reading is ended by CR/LF or by LF.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 9 Jan 2014, 00:59 last edited by
                  #8

                  Hi,

                  If you use a text stream, or if you use QIODevice::Text when you open the file, Qt will convert all CR/LF to LF.

                  To preserve them, you will need to read your data as raw bytes, and then search for "\r\n" manually.

                  See http://qt-project.org/doc/qt-5/qfile.html#reading-files-directly

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0

                  6/8

                  8 Jan 2014, 18:53

                  • Login

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