View Full Version : QDataStream interprets newline as end of file

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);
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))
QDataStream in(&file);
in >> check_magic;
if (check_magic != magic)
return false;

in >> check_version;
if (check_version != version)
return false;
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.

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

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

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).

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++) {
in >> foo;

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;
} // after all foos are done


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.

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

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.

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.

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