PDA

View Full Version : How to read a value from tow columns file !



mejacky
4th October 2015, 13:13
Hallo Guys,

I have a txt-File, which contains values like that :

distance force
0.090721 0.0072147
0.181527 0.0139748
0.270888 0.0203682
0.360796 0.0262649
0.451177 0.0317043
0.541753 0.0366577
0.632632 0.0409662
0.724928 0.0451153

I just need to read the tow values of the last line but separately. For example: if i pressed a Qpushbutton (1) i get 0.724928, and if i pressed a Qpushbutton (2) i get 0.0451153 .

i've tried the following code, but i always get the first value of the last line (0.724928)..my question is how to get the second value of the last line (0.0451153) ?

QFile inputFile("measurnments.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
this->display->setText(line);
}
inputFile.close();
}

I'm just a Newbie :), so i'll be grateful und i'll appreciate it if somone could help me


Best Reagrds

prasad_N
4th October 2015, 13:37
readLine() reads complete line, you need to split that line based on space.

QStringList list = line.split(" ");
Now list.at(0) contains 1st value
list.at(1) contains 2nd value

mejacky
4th October 2015, 14:16
first of all thx a lot for the quick reply,

i've tried the QStringList list = line.split(" "), but when i leave a space between " " i get the following Debug Error:


114121141211412

when i write QStringList list = line.split("") without a space, and then write for example list.at(2), i get (.) which is the comma in 0.724928 and when i write for example list.at(5), i get 4 which is the fifth chracter in 0.724928 .

can you tell me please, where i'm going wrong

anda_skoa
4th October 2015, 14:55
list.at(2) is not a valid, your list only contains two elements: the number before the space and the number after the space.
So the only valid indexes are 0 and 1

Cheers,
_

mejacky
4th October 2015, 15:07
hey..but as i wrote when i write list.at(0) i get just the first character (0) of the numer 0.724928 and when i write list.at(1) i get (.) :(

do you have any idea why is that happening ?

Regards

prasad_N
4th October 2015, 15:18
This should not happend unless you have spces between numbers or else you might doing line.at(0) or line.at(1).
show your code.

mejacky
4th October 2015, 15:23
i think every chracter is being interpreted as a column or an element und not the whole number 0.724928.. so if i give list.at(15) i get 1 from the other number 0.0451153..

so how could i solve the problem ? :(

prasad_N
4th October 2015, 15:27
Show your splitting part of the code plz

mejacky
4th October 2015, 15:31
i'm sorry but which code do you mean ?

how i saved the values or how iam i reading them ?

File inputFile("measurnments.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
QStringList list = line.split("");
QString val = list.at(1);
this->display->setText(val);
}
inputFile.close();
}

prasad_N
4th October 2015, 15:31
QStringList list = line.split(" ");
list.at(0)
list.at(1)
Will surly work man.
//show this part if your doing in diff way

mejacky
4th October 2015, 15:32
QFile inputFile("measurnments.txt");
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
QStringList list = line.split("");
QString val = list.at(1);
this->display->setText(val);
}
inputFile.close();
}

prasad_N
4th October 2015, 15:46
line.split(" "); will do.
If you still have problem print & check what you are reading into line before split it.

mejacky
4th October 2015, 15:52
i'm still having the same problem..i'm sorry but what do you mean by print & check into line ?

prasad_N
4th October 2015, 15:55
After
Qstring line = in.readLine();
qDebug() << line; //see what your are reading into line

anda_skoa
4th October 2015, 16:45
hey..but as i wrote when i write list.at(0) i get just the first character (0) of the numer 0.724928 and when i write list.at(1) i get (.) :(

And as you wrote you get that when you are not using the space separator to split on.

So prasad_N made a typo in an otherwise good answer. As a programmer you could have looked at the error, then figured that one of your two list.at() calls must be the problem, then checked which one and then fixed it after consulting the documentation.

But instead you randomly changed something else, getting a totally different and for your purpose unusable result.

One of the most important skills for a developer is to anaylize error situations.
Asking for help for every minor problem is not viable in the long run.

Cheers,
_

mejacky
5th October 2015, 20:24
Thank you a lot guys for your replys..i'm still getting the same Probleme, but I will keep trying until it works


Best Regards

ChrisW67
5th October 2015, 21:04
You have been given the solution for the by-character, rather than by-column, split. If you have done that change, that is replaced

QStringList list = line.split("")
// with
QStringList list = line.split(" ")
, and the character between the columns is indeed a space (not a Tab) then you must be seeing a different problem. You are not to describing the new problem, and doing so would definitely help.

mejacky
6th October 2015, 12:28
hey Chris...i've tried everything actually, but whenever i leave a space or tab between the "", i become this failure ( QList index out of range )..i really don't know how this could be solved !

Error:"ASSERT failure in QList::at: "index out of range", file ../../../../Qt/2010.05/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 455 Invalid parameter passed to C runtime function. Invalid parameter passed to C runtime function."

Added after 29 minutes:

wow Chris thx a lot...actually it was a tab and not a space :), cause according to the way how i saved the values i always put a tab between them...now it works perfect

i've noticed that with help from qdebug() ..thx prasad_N :)

i really appericiate all your replys

Best reagrds from germany :)

ChrisW67
6th October 2015, 20:59
You can also use the regular expression variant of the split() function to split on any whitespace:


str = "Some text\t\twith strange whitespace.";
list = str.split(QRegExp("\\s+"));

jefftee
10th October 2015, 00:46
i've noticed that with help from qdebug() ..thx prasad_N :)
I hope you don't mind me posting, but you really should spend some time and learn how to set breakpoints and inspect variables using the Qt Creator debugger. This could have been solved very quickly had you simply set a breakpoint after you split the line and inspect your QStringList. You would have noticed that the line was actually not split and consisted of a single value and the QStringList count would have been 1.

Once you become good at debugging, you'll save tons of time solving trivial problems. You'll never solve more complex problems with the shotgun approach in my experience.

Good luck!