Read In and Print .dat file
-
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!
-
This post is deleted!
-
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!
@WesLow
YourQTextStream
code reads adouble
from a file provided it is there in text, like say1.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.... -
Hi
Can you show how the .dat file looks inside? -
@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.