PDA

View Full Version : How to read numbers from a File and store in vector



rdelgado
8th February 2011, 18:58
Hello,

I wish to know how to read numbers from a file and store them in something like a vector.

My file is a .txt with numbers in rows and columns:

12 34 567 423 788
23 343 45 56 67 78
...

I've already been able to read the file using:



QFile myFile(file);

if(!myFile.open(QFile::ReadOnly))
{
qDebug() << "Cannot open...";
return;
}

QTextStream input(&myFile);
QString line;

line = input.readLine();

qDebug() << line;


It displays correctly the line of my file.
Now, I need to store each numerical value of the file to a vector or similar, so I can do operations over those numbers.
Something like:



int VectorFirstLine[5];

qDebug() << VectorFirstLine[3];

VectorFirstLine[5]*AnotherVariable;



And so on... What is the best way to do that? Using a QtList? a QVector?

Thank you very much!

high_flyer
8th February 2011, 19:33
Here are some hints:
- You can split strings using QString::split()
- You can covert strings to ints using QString::toInt()

ChrisW67
8th February 2011, 23:02
The QList page discusses the different containers.

Another hint:
You can create two-dimensional arrays of items using a list or lists or vector of vectors approach.

bramadani
18th February 2011, 11:38
What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?




double *Hydraulics::elevationy(int im) {

ifstream bin("elevy.in");

double *hpi = new double [im+1];

for(int i=1; i<=im; i++)
bin>>hpi[i];

bin.close();
return hpi;

}

where elevy.in is the input file, of course :)

high_flyer
18th February 2011, 12:24
Have a look at QDataStream and QVector and try to figure it out.
Ask again if you get stuck.

bramadani
18th February 2011, 14:12
I tried to but failed...

Would you please write the Qt equivalent please?

thank you in advance

high_flyer
18th February 2011, 14:20
I tried to but failed...
Show your code.

SixDegrees
18th February 2011, 15:08
What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?




double *Hydraulics::elevationy(int im) {

ifstream bin("elevy.in");

double *hpi = new double [im+1];

for(int i=1; i<=im; i++)
bin>>hpi[i];

bin.close();
return hpi;

}

where elevy.in is the input file, of course :)

In the general case, this code will not read a column of numbers from a file. It will only handle the degenerate case where the files contains a single column of numbers. Even then, it won't work unless the count passed exactly matches the number of numbers in the file.

From a C/C++ perspective, there are also indexing issues; array indexing normaly starts with zero.

high_flyer
18th February 2011, 15:18
Look, if you want help you can at least TRY to help us help you.
Your original question was:

What is the Qt equivalent of the following C++ procedure of reading a column of numbers from a file and storing them to a vector?
I then suggested to you two Qt classes to be used for converting the code you posted to its Qt equivalent.
On which you answered you have tried it, but it didn't work.
However the code you reposted is your old code which is NOT using Qt classes - so what have you tried?


In the general case, this code will not read a column of numbers from a file. It will only handle the degenerate case where the files contains a single column of numbers. Even then, it won't work unless the count passed exactly matches the number of numbers in the file.

From a C/C++ perspective, there are also indexing issues; array indexing normaly starts with zero.
This is another issue.
Lets do it one step at a time.

bramadani
18th February 2011, 15:31
Ok, so here is the code which I tried using Qt classes



double *Hydraulics::elevationx(int im) {

double xpi[im+1];
QFile bin("elevx.in");
QTextStream input(&bin);
QString line;
line = input.readLine().split();
xpi = line.toDouble();

return xpi;
}

high_flyer
18th February 2011, 16:33
This code can't possibly compile because split must take at least one parameter, and it returns a QStringList and not a QString.

It seems you have no interest in solving this your self.
In this forum we like to help as much as we can, but don't expect us to do you work and thinking for you.

Once you have a real problem which doesn't boils down to "do this for me", you are welcome to try again.

SixDegrees
18th February 2011, 17:38
This is another issue.
Lets do it one step at a time.

Step 0: Make a clear, precise statement of the problem to be solved. :-)

Here, it isn't at all clear what sort of file is to be read; there seems to be an assumption, but I've found through bitter experience that assumptions are often wrong.

ChrisW67
20th February 2011, 23:03
Read the comment I have placed in your code:

Ok, so here is the code which I tried using Qt classes



double *Hydraulics::elevationx(int im) {

double xpi[im+1];
// You want storage for im doubles but allocated storage for one more than that

QFile bin("elevx.in");
// excellent, but you need to do something to open the file for reading

QTextStream input(&bin);
// fine, at least if the file is open

QString line; // ok
line = input.readLine().split();
// QString::split() needs and argument and does not return QString
// Your original C++ code reads each line as a single number, you don't need split() to do that here.

xpi = line.toDouble();
// Do you care if the conversion fails?
// xpi is a pointer to an array of doubles, not a double.

// Did you want to do this only for the first line of the file, or for every line in the file?
// Your original C++ had a loop construct
// What happens if the file is longer or shorter than "im" elements?

return xpi;
// The storage for xpi[] is deallocated at the end of this function... this is bad for the receiving code which gets a pointer to a block of random memory
}


You should look at std::vector or QVector as better methods of storing arrays of values.

bramadani
21st February 2011, 16:49
Thanks for the comments and the help

I tried the following code and it managed to compile, hope it does what I need



double *Hydraulics::elevationx(int im) {

double *xpi = new double[im+1];
QFile bin("elevx.in");
QTextStream input(&bin);
QString line;
int i = 1;

while(!input.atEnd()) {
line = input.readLine();
xpi[i] = line.toDouble();
i++;
}
return xpi;
}

high_flyer
21st February 2011, 17:09
This code will work only if it is guaranteed that the file format is such, that each line contains nothing more than a numeric value and a line break.
You have completly ignore many important comments the previous poster had put in your code - such as opening the file and many others.
Also its is recommended to use container classes instead of C type arrays, such as QVector or QList.

bramadani
22nd February 2011, 10:29
Yes, so far the files are guaranteed to be in a coulmn format line

234
145532
123
35245
342
76
7546
.
.
324

and so on

However I am concerned what if the files are separated with comma values but not in the same distance

e.g.

0 , 54
15 , 65
20 , 43
25 ,64
30,100

I know that for these cases we use the split(); function however if we say take the argument as split(","); then the strings will include the empty spaces as well, right? Is there any way out of this?

Thank you!

stampede
22nd February 2011, 10:34
You can use QString::trimmed (http://doc.qt.nokia.com/latest/qstring.html#trimmed) to remove white spaces from the start and the end of a string.