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?
Forum Updated to NodeBB v4.3 + New Features

Remove newline from QByteArray?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.6k 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by A Former User
    #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 last edited by A Former User
      #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
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on last edited by pauledd
        #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 last edited by A Former User
          #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
          • pauleddP Offline
            pauleddP Offline
            pauledd
            wrote on last edited by
            #5

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

            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