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. Remove newline from QByteArray?
QtWS25 Last Chance

Remove newline from QByteArray?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.4k 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.
  • P Offline
    P Offline
    pauledd
    wrote on 12 Mar 2017, 08:50 last edited by A Former User 3 Dec 2017, 08:57
    #1

    Hello,
    My task is to read a csv file into a struct.
    sample line:

    2017-03-12 07:50:01,17.7,1022.6,0.1,93.9
    

    The csv contains only doubles and the struct also (the date is irrelevant). The problem I have is that I cant read the last value because the csv contains a newline at the end of every line.

    struct Wstruct{
    	double tempIn;
            double press;
    	double tempOut;
    	double humi;
    };
    ...
    private:
     Wstruct values;
    ...
    QByteArray line = file.readLine();
    
    values.tempIn = line.split(',').at(1).toDouble();
    values.press = line.split(',').at(2).toDouble();
    values.tempOut = line.split(',').at(3).toDouble();
    values.humi = line.split(',').at(4).toDouble();
    
    

    While the first three values work the last one is always zero.
    How can I remove the newline before assigning it to "values.humi"?
    I tried to use ".remove(QRegExp())" on "line" but this doesnt work because
    line is a QByteArray and not a QString.
    Any ideas?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 12 Mar 2017, 09:02 last edited by A Former User 3 Dec 2017, 09:20
      #2

      Hi! You could use readLine():

      QFile file("in.csv");
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;
      QTextStream in(&file);
      while (!in.atEnd()) {
          const QString = in.readLine();
          // do something
      }
      
      1 Reply Last reply
      1
      • P Offline
        P Offline
        pauledd
        wrote on 12 Mar 2017, 09:06 last edited by pauledd 3 Dec 2017, 09:06
        #3

        Oh thanks!

        I found this works too:

        values.humi = QString(line.split(',').at(4)).remove(QRegExp("\\n")).toDouble();
        

        Is my approach also ok?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 12 Mar 2017, 09:09 last edited by A Former User 3 Dec 2017, 09:12
          #4

          Yes, it's okay, too. Only keep in mind, that with readAll you'll create a copy of the whole file in RAM. Depending on the size of the file and your hardware, that might be a problem.

          Edit: Sorry, I think I didn't read your first post properly. You don't use readAll. My fault, forget it :-)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pauledd
            wrote on 12 Mar 2017, 09:16 last edited by
            #5

            ok, thank you, the csv would not be longer than 300 lines anyway ;)

            1 Reply Last reply
            0

            5/5

            12 Mar 2017, 09:16

            • Login

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