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 In and Print .dat file
Forum Updated to NodeBB v4.3 + New Features

Read In and Print .dat file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 526 Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 18 Oct 2021, 15:08 last edited by
    #1

    Hello, all! Currently, I am trying to read in a .dat file and plot the information on a line chart. The plotting part is not difficult; however, I would like to know how to read in a the components of a .dat file so I can plot them one at a time. Here is one block of code I have tried:

    #include <QFile>
    #include <QTextStream>
    
    QFile f("your.file");
    f.open(QIODevice::ReadOnly);
    QTextStream s(&f);
    
    while ( !s.atEnd() ) {
      double d;
      s >> d;
      // append d to some list;
    }
    

    Source: https://stackoverflow.com/questions/33621771/read-the-data-from-text-file-in-qt

    Am I getting nothing because there is an error in this block of code, or does this code look fine? (meaning I must have forgot to connect an interface element). Thank you, in advance, for your help!

    J 1 Reply Last reply 18 Oct 2021, 15:53
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 18 Oct 2021, 15:09 last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on 18 Oct 2021, 15:40 last edited by
        #3

        Is a.dat file a text file or a binary file ?

        1 Reply Last reply
        4
        • ? A Former User
          18 Oct 2021, 15:08

          Hello, all! Currently, I am trying to read in a .dat file and plot the information on a line chart. The plotting part is not difficult; however, I would like to know how to read in a the components of a .dat file so I can plot them one at a time. Here is one block of code I have tried:

          #include <QFile>
          #include <QTextStream>
          
          QFile f("your.file");
          f.open(QIODevice::ReadOnly);
          QTextStream s(&f);
          
          while ( !s.atEnd() ) {
            double d;
            s >> d;
            // append d to some list;
          }
          

          Source: https://stackoverflow.com/questions/33621771/read-the-data-from-text-file-in-qt

          Am I getting nothing because there is an error in this block of code, or does this code look fine? (meaning I must have forgot to connect an interface element). Thank you, in advance, for your help!

          J Online
          J Online
          JonB
          wrote on 18 Oct 2021, 15:53 last edited by JonB
          #4

          @WesLow
          Your QTextStream code reads a double from a file provided it is there in text, like say 1.23, those actual characters You cannot possibly know whether this will work till you know the format of your .dat input file. Somehow I suspect it does not present the text of a floating point number as its first data item....

          1 Reply Last reply
          2
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 18 Oct 2021, 16:07 last edited by
            #5

            Hi
            Can you show how the .dat file looks inside?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on 19 Oct 2021, 03:45 last edited by
              #6

              @WesLow said in Read In and Print .dat file:

              does this code look fine?

              Not really. Quite apart from the file not containing what you expect (as others have said), this could be failing because the file is not found or not openable. You should at least check the return value from QFile::open(). If that is false then QFile::exists() and QFileInfo::absoluteFilePath() could be useful to understand why.

              1 Reply Last reply
              1

              4/6

              18 Oct 2021, 15:53

              • Login

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