PDA

View Full Version : Saving values to an external file



mefadime
8th July 2015, 20:20
Hallo Guys,

I'm a totally beginner in Qt and in Programming in general. I'm trying to save measurement values to an external file, so i can plote them later.

for example: the table should be something like that.

Values1 Values2

10 33
32 43
12 22
. .
. .
. .

33 65


i' would really appreciate it if someone could help me.

Regards

wysota
8th July 2015, 20:32
Use QFile optionally with QTextStream.

mefadime
9th July 2015, 11:20
allo Guys,

I'm a totally beginner in Qt and in Programming in general. I'm trying to save measurement values to an external file, so i can plote them later.

for example: the table should be something like that.

Values1 Values2

10 33
32 43
12 22
. .
. .
. .

33 65


i' would really appreciate it if someone could help me.

Regards

stampede
9th July 2015, 11:37
You have already created a thread here http://www.qtcentre.org/threads/63045-Saving-values-to-an-external-file
What have you tried ?
Btw. if you are new to programming, then starting with a multiplatform (not only) GUI toolkit with rather complicated build procedure is not the best way to learn. Why don't you start with something like Ruby or python or even plain C++ to get the basics first ?

mefadime
9th July 2015, 12:06
Thx for replying,

i know how save date to an external file using QFile. but what i'm looking for is how to make a table like this:

Values1 Values2

10 33
32 43
12 22
. .
. .
. .

33 65

:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::

i wrote this code but it didn't work

bool UserInterface::readFile() {
QFile file("measurnment.txt");
QDatasavingeam in(&file);
if (!file.open(QIODevice::ReadOnly)) {

return -1;
}

quint16 row;
quint16 column;
QString saving;

while (!in.atEnd()) {
in >> row >> column >> saving;
if ( !column%2 ) value[row] = saving;
else value[row] = saving.toInt();
}
file.close();
return 1;
}

bool UserInterface::writeFile() {
QFile file("measurnment.txt");
if (!file.open(QIODevice::WriteOnly)) {

return -1;
}

QDatasavingeam out(&file);

for (int row = 0; row < 10; ++row) {
for (int column = 0; column < 2; ++column) {
if ( !column%2 ) {
value[row]=Force_value;
QString saving = QString::number(value[row]);
out << quint16(row) << quint16(column) << saving;
}


else {
value[row]=Deformation_value;
QString saving = QString::number(value[row]);
out << quint16(row) << quint16(column) << saving;
}

}
}
file.close();
return 1;
}
}

i hope that i made my que clear now

Regards

mefadime
9th July 2015, 12:15
yes it is a repost, cause i thought that the first one was deleted ! i've also tried to delete this post but it didn't work.

yes i'm new to programming but i know the basics and more for sure, otherwise i wouldn't post mz que here. so if you could help it would be nice.

i don't know which level of que. do you expect, but since we are in Newbie threads, then i think everzthing is possible, isn't is ? even if the que so simple for you and maype silly, but for other people is not :)

so my que is :

i want to make a table like this:

Values1 Values2

10 33
32 43
12 22
. .
. .
. .

33 65

:::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::

i wrote this code but it didn't work

bool UserInterface::readFile() {
QFile file("measurnment.txt");
QDatasavingeam in(&file);
if (!file.open(QIODevice::ReadOnly)) {

return -1;
}

quint16 row;
quint16 column;
QString saving;

while (!in.atEnd()) {
in >> row >> column >> saving;
if ( !column%2 ) value[row] = saving;
else value[row] = saving.toInt();
}
file.close();
return 1;
}

bool UserInterface::writeFile() {
QFile file("measurnment.txt");
if (!file.open(QIODevice::WriteOnly)) {

return -1;
}

QDatasavingeam out(&file);

for (int row = 0; row < 10; ++row) {
for (int column = 0; column < 2; ++column) {
if ( !column%2 ) {
value[row]=Force_value;
QString saving = QString::number(value[row]);
out << quint16(row) << quint16(column) << saving;
}


else {
value[row]=Deformation_value;
QString saving = QString::number(value[row]);
out << quint16(row) << quint16(column) << saving;
}

}
}
file.close();
return 1;
}
}

i hope that i made my que clear now :)

Regards

anda_skoa
9th July 2015, 12:25
Threads merged

Cheers,
_

wysota
9th July 2015, 19:53
Thx for replying,

i know how save date to an external file using QFile. but what i'm looking for is how to make a table like this:

Values1 Values2

10 33
32 43
12 22
. .
. .
. .

33 65

You write header of the first column, then a tabulator, then header of the second column, newline, newline, value of the first column of the first row, tabulator, value of the second column, newline and so on.

As for your code, it will not even compile so it has no chance of working.

mefadime
9th July 2015, 21:16
yes it didn't work..is there any other suggestion which is more useful than ,, it has no chance of working"..if someone could help me at least with a reference or tip it would be nice, otherwise just delete my post please.

stampede
9th July 2015, 21:39
You don't need the internal loop over the two values for the column, it makes the code look messy and difficult to read. You can simply iterate over the rows and hard-code the exact values you want to write. Now every second iteration over "column" value overwrites the "values[row]" written by the first iteration. Clean up the code a bit and it should be easier to spot the errors.

wysota
10th July 2015, 07:21
yes it didn't work..is there any other suggestion which is more useful than ,, it has no chance of working"..if someone could help me at least with a reference or tip it would be nice, otherwise just delete my post please.

I would start by changing "QDatasavingeam" into something which actually exists, so that your code builds. I already suggested you two classes you could use. You used one of them, what about the other one?

mefadime
13th July 2015, 15:29
i wrote a new code after a lot of reading and trying :) and it works now..i wanted to delete this post but i coudn't find out how to do this, can someone tell me how please