PDA

View Full Version : read a .txt file and store it in a double array



fatecasino
3rd December 2010, 02:08
Hi there,
supposing that i a have a data.txt with two columns of doubles,

23 4.43
1.2 232
3.444 23.23
......... (we don't know the data size until we open the file)

and I want to store it (dynamically) in a 2d array.
How can i do it in Qt? I read several posts in the forum and I got really confused!

PS:what's the simplest way to print out the array to confirm that the data was read correctly? in c++ it was simply cout, in Qt?

ChrisW67
3rd December 2010, 05:47
You could use a QVector<QVector<double, double> >, QVector<QVector2D>, or QVector<PointF> depending on what makes most sense. You could also use QList<> as the outer container. QPair<double, double> may also be useful.

Using Qt does not change anything regarding the features of C++. You may use std::cout for for debugging duties, or you can use qDebug() and related functions from Qt.

fatecasino
3rd December 2010, 16:28
hi ChrisW67,

thanks for your answer. Supposingly this problem was simple in C++, but now I have spent around 12 hours of searching and still haven't got it!
Hence:
Part A:I should read in the data from file
Should i do read it line by line or store it directly to variable. Back in C++ I used to do:


ifstream in(FileName_Read);
while(getline(in,line,'\n')) {

// Declarations.
stringstream ss;
ss << line;
string entry;

while (getline(ss,entry,delimiter)) {

// Declarations.
stringstream entry_ss;
float entry_float;
char entry_char;

// Insert data entry into stringstream.
entry_ss << entry;

// Convert data entry to float.
entry_ss >> entry_float;
I suppose in Qt there are lots of overloaded functions and classes that let you do this in 3-4 lines, but I haven't found them yet :confused:
Part B: Store the data to a local variable
So far in Qt I was using C++ double pointers,I hadn't discovered QVector. I will check it out now.

PS:isn't it strange that when you google Qt problems you are almost never sent to qtcentre forums?? Actually, you have to use qtcentre search machine to find what you need and that takes a lot of time.

high_flyer
3rd December 2010, 16:55
Back in C++ I used to do:
Lets get this straight:
Qt IS C++ code!
Just like you don't differentiate between STL or boost and c++, so you don't with Qt.
Do you understand the concept of a library?


Should i do read it line by line or store it directly to variable.
Reading line by line is still just reading.
Storing in something else - they are not exclusive.


suppose in Qt there are lots of overloaded functions and classes that let you do this
What is 'this'?
Reading from a file?
Storing your data in an array?
All of the above?

And you are right - there are a lot of Classes in Qt.

but I haven't found them yet
Here is the full list:
http://doc.trolltech.com/4.7/classes.html

It seems you are aware of streams, as you have posted in your code.
So why not start for example by searching for streams in the Qt documentation?
If you are aware of streams, you probably are also aware of vectors, which you could also look in the Qt docs.
Usually the Qt classes have very sensible name, so searching for "file" in the class list will give you some good hits to what you might be looking for, if for example you need some file handling classes.
Qt has many STL classes reimplemented, take the time to read the documentation.

fatecasino
3rd December 2010, 20:07
What is 'this'?
Reading from a file?
Storing your data in an array?
All of the above?
reading the file->taking the actual data that you need->passing the data to a vector/array


The Documentation is fantastic, but there are tens of classes and functions related to "file". There is no reason to start checking each one of them.Knowledge is not built in this way.
I started this thread expecting something like:

for reading a file, try using Qxxxxx
for obtaining the actual data that you need from the file, try using Qzzzzz
for passing this data to a vector/array, try using Qrrrrrrr

Then I would go directly to The Documentation to learn how to use Qxxxxx,Qzzzzz,Qrrrrrrr

marcvanriet
3rd December 2010, 20:13
Hi,

In addition to using the 'old' C++ streams, you have the option to use QFile and QTextStream. Directly from the Qt documentation :


QFile file("in.txt");
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
// process your line here
}

Then, you can use QString::Split to split your line with two numbers into 2 strings, and QString::toDouble() to convert it to a double. The previously mentioned vector and list classes can be used to store the points.

Best regards,
Marc