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 Update on Tuesday, May 27th 2025

Read In and Print .dat file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 511 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 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!

    JonBJ 1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

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

        1 Reply Last reply
        4
        • ? A Former User

          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!

          JonBJ Online
          JonBJ Online
          JonB
          wrote on 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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 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

              • Login

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