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 to use QFile, QByteArray and QString?
QtWS25 Last Chance

How to use QFile, QByteArray and QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 3 Posters 4.6k 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 victor wang

    Hi, i'm using Qt5.5 in my computer.
    I'm programming a code that can read what i write in the EEprom.
    I just wanna read the valid ASCII out instead of all of the value in EEprom.
    This is my code.

    I had try to use not only readLine()、Read() but also readAll().
    But none of it do what i want.
    This is the picture that i cat from my eeprom here.

    I just want to read the word "o" in the beginning and "6" in the end.
    What can i so ?
    Please help!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @victor-wang What exactly does not work?
    It is just a file - so just read and parse it as any other file.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    V 1 Reply Last reply
    0
    • jsulmJ jsulm

      @victor-wang What exactly does not work?
      It is just a file - so just read and parse it as any other file.

      V Offline
      V Offline
      victor wang
      wrote on last edited by victor wang
      #3

      @jsulm
      When i read it out it will read not only words but also the unknown symbol below the end of the word "6".

      Like this picture shows what it shows.

      how can i get avoid from the unknown words at the end?

      jsulmJ 1 Reply Last reply
      0
      • V victor wang

        @jsulm
        When i read it out it will read not only words but also the unknown symbol below the end of the word "6".

        Like this picture shows what it shows.

        how can i get avoid from the unknown words at the end?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @victor-wang It depends on what exactly you want to get from the file. You can read line by line until you get what you want and then stop.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        V 1 Reply Last reply
        0
        • jsulmJ jsulm

          @victor-wang It depends on what exactly you want to get from the file. You can read line by line until you get what you want and then stop.

          V Offline
          V Offline
          victor wang
          wrote on last edited by
          #5

          @jsulm
          I've used readLine() but there are some quest.
          1.How can i know it is the end of the word?
          2.Will it know by itself?

          jsulmJ 1 Reply Last reply
          0
          • V victor wang

            @jsulm
            I've used readLine() but there are some quest.
            1.How can i know it is the end of the word?
            2.Will it know by itself?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @victor-wang What does "word" mean? It will not know by itself, you should know.
            Can you explain EXACTLY what you want to read from the file? It is really unclear.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            V 1 Reply Last reply
            0
            • jsulmJ jsulm

              @victor-wang What does "word" mean? It will not know by itself, you should know.
              Can you explain EXACTLY what you want to read from the file? It is really unclear.

              V Offline
              V Offline
              victor wang
              wrote on last edited by victor wang
              #7

              @jsulm
              I just wanna read this.
              Not the black question marks.

              jsulmJ 1 Reply Last reply
              0
              • V victor wang

                @jsulm
                I just wanna read this.
                Not the black question marks.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @victor-wang And what is the problem? Just read line by line the first 6 lines.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                V 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @victor-wang And what is the problem? Just read line by line the first 6 lines.

                  V Offline
                  V Offline
                  victor wang
                  wrote on last edited by victor wang
                  #9

                  @jsulm
                  That because if there is other person who want to write things into eeprom.
                  I will show what he write in the eeprom.
                  According to this addition, I can't just read for six lines.
                  Because not every one will just write 6 lines.

                  I have to know where is the last word.

                  jsulmJ 1 Reply Last reply
                  0
                  • V victor wang

                    @jsulm
                    That because if there is other person who want to write things into eeprom.
                    I will show what he write in the eeprom.
                    According to this addition, I can't just read for six lines.
                    Because not every one will just write 6 lines.

                    I have to know where is the last word.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @victor-wang Again: what is "last word"? How is it defined? I still don't understand what information you want to get. Are those MAC2, Model, PN, ...?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    V 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @victor-wang Again: what is "last word"? How is it defined? I still don't understand what information you want to get. Are those MAC2, Model, PN, ...?

                      V Offline
                      V Offline
                      victor wang
                      wrote on last edited by victor wang
                      #11

                      @jsulm

                      For example the picture i gave you.
                      The last word means the last line of the "6".
                      Picture here

                      Those words is what i write in eeprom.
                      For other example if i write "skdjb,smdnv6546321" into eeprom.
                      Then the last Alphabet is "1" and i just want to read what i write.

                      jsulmJ 1 Reply Last reply
                      0
                      • V victor wang

                        @jsulm

                        For example the picture i gave you.
                        The last word means the last line of the "6".
                        Picture here

                        Those words is what i write in eeprom.
                        For other example if i write "skdjb,smdnv6546321" into eeprom.
                        Then the last Alphabet is "1" and i just want to read what i write.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @victor-wang Then read line by line and parse each line to check whether it is what you want.
                        And you should use correct wording, else it's hard to understand what you mean: a word is not a line.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        V 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @victor-wang Then read line by line and parse each line to check whether it is what you want.
                          And you should use correct wording, else it's hard to understand what you mean: a word is not a line.

                          V Offline
                          V Offline
                          victor wang
                          wrote on last edited by
                          #13

                          @jsulm
                          Sorry, how to parse it?
                          I will not know what will other people write, how can parse?

                          jsulmJ 1 Reply Last reply
                          0
                          • V victor wang

                            @jsulm
                            Sorry, how to parse it?
                            I will not know what will other people write, how can parse?

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @victor-wang OK, you don't know what others are writing, right? But you know what you want to read, right?
                            If so then:

                            QByteArray whatIWantToRead = "ABCD";
                            QString line = file.readLine();
                            if (line == whatIWantToRead)
                                // do something
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            V 1 Reply Last reply
                            1
                            • jsulmJ jsulm

                              @victor-wang OK, you don't know what others are writing, right? But you know what you want to read, right?
                              If so then:

                              QByteArray whatIWantToRead = "ABCD";
                              QString line = file.readLine();
                              if (line == whatIWantToRead)
                                  // do something
                              
                              V Offline
                              V Offline
                              victor wang
                              wrote on last edited by
                              #15

                              @jsulm
                              If i want to read a~z、A~Z and some other exclamation mark and question mark and etc..
                              I have to write all of it into whatIWantToRead ?

                              jsulmJ 1 Reply Last reply
                              0
                              • V victor wang

                                @jsulm
                                If i want to read a~z、A~Z and some other exclamation mark and question mark and etc..
                                I have to write all of it into whatIWantToRead ?

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @victor-wang You can use regular expressions, see https://en.wikipedia.org/wiki/Regular_expression and http://doc.qt.io/qt-5.8/qregularexpression.html

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                V 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @victor-wang You can use regular expressions, see https://en.wikipedia.org/wiki/Regular_expression and http://doc.qt.io/qt-5.8/qregularexpression.html

                                  V Offline
                                  V Offline
                                  victor wang
                                  wrote on last edited by
                                  #17

                                  @jsulm
                                  Do you have an example?
                                  Or can i parse it by ASCII ?

                                  jsulmJ J.HilkJ 2 Replies Last reply
                                  0
                                  • V victor wang

                                    @jsulm
                                    Do you have an example?
                                    Or can i parse it by ASCII ?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @victor-wang There are a lot of examples for regular expressions on the Internet

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • V victor wang

                                      @jsulm
                                      Do you have an example?
                                      Or can i parse it by ASCII ?

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #19

                                      @victor-wang

                                      If you know, that valid lines always contain legit chars between 'A' and 'z'

                                      You can make a simple If-statement with the first char in your bytearray:

                                      QByteArray bArray = file.readline();
                                      if(bArray[0] < QLatin1Char('A') || bArray[0] > QLatin1Char('z'))
                                           //Abort condition met
                                      

                                      use QLatin1Char if its ASCII, QChar if its unicode.


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      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