PDA

View Full Version : QDataStream interprets newline as end of file



jovin
18th September 2012, 20:07
Good evening,

I write some data into a file with QDataStream.

QDataStream out(&file);
out << quint32(magic);
out << qint32(version);
out.setVersion(QDataStream::Qt_4_8);
for (int i = 0; i < data.size(); i++)
{
out << data[i].a;
out << data[i].b;
...
out << endl;
}


Now, when reading the file from the same program; it works flawlessly.


QFile file(PATH + filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QDataStream in(&file);
in >> check_magic;
if (check_magic != magic)
return false;

in >> check_version;
if (check_version != version)
return false;
in.setVersion(QDataStream::Qt_4_8);
for (int i = o; i < data.size(); i++)
{
in >> data[i].a;
...
in.skipRawData(1); // otherwise the newline makes it messy
}

When I want to read the file from another program; it stops after the first iteration.
I placed a while(!(file.atEnd()) into my other program, to see how far it comes,
and it stops right after the endline.

I do not understand how that is possible, I use the same code for reading in both programs.

norobro
18th September 2012, 21:21
Try using QDataStream::atEnd() instead of QFile.atEnd().

jovin
23rd September 2012, 20:18
Still the same.
After the first iteration it stops.

wysota
23rd September 2012, 20:37
Do you realize that endl is part of QTextStream (or std namespace) and it doesn't make any sense to use it with QDataStream?

Furthermore I wouldn't be suprised if serializing endl on Windows would actually store two bytes (\r\n).

jovin
23rd September 2012, 20:52
I already removed that part. However, it's still only reading the first iteration.
But the same process works for the program which writes the file!
The only difference in the reading process is how I store the data.

The program which writes and reads the file;
A temp QList takes all values as QStrings. Before the next iteration starts I append the temp QList to the real thing.

for (int i = 0; i < foo.size(); i++) {
temp_list.clear()
in >> foo;
temp_list.append(foo)
...
...
RealFoo.append(temp_list)
}


The program which only reads the file;
I have a QVector<foo> foos of the appropriate size.
Each iteration I take the value in a temp_string and immediately call a setter method for the foo object.

for (int i = 0; i < foo.size(); i++) {
in >> temp_string;
foos[i]->set_a(temp_string.toFoo());
...
...
} // after all foos are done

RealFoo.load_foos(foos);

Here only the first iteration is a success, because it ends after the first.

Edit: Well, I guess the thread title is totally offtopic now.

wysota
24th September 2012, 00:12
But are you sure you should be using QDataStream at all?

jovin
24th September 2012, 13:46
Well, at least it makes my data unreadable.

If the reading in general would fail, then I would use something different. However, it is working for one of my programs,
so it can't be that wrong to use QDataStream.

The error has to be somewhere else. I'll try to post a more detailed example later.

jovin
24th September 2012, 23:23
It's getting interesting.

When I do;

for (int i = 0; i < size; i++) {
while(!in.atEnd()) {
in >> temp;
qDebug() << temp << endl;
}
}
The entire file is being printed !!!

I have to use two loops to get all data. How does that come ??!
It's really strange, because it's working for the other program with only one loop.

wysota
24th September 2012, 23:57
Show us the whole function.