Results 1 to 9 of 9

Thread: QDataStream interprets newline as end of file

  1. #1
    Join Date
    Mar 2012
    Location
    Germany
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataStream interprets newline as end of file

    Good evening,

    I write some data into a file with QDataStream.
    Qt Code:
    1. QDataStream out(&file);
    2. out << quint32(magic);
    3. out << qint32(version);
    4. out.setVersion(QDataStream::Qt_4_8);
    5. for (int i = 0; i < data.size(); i++)
    6. {
    7. out << data[i].a;
    8. out << data[i].b;
    9. ...
    10. out << endl;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Now, when reading the file from the same program; it works flawlessly.
    Qt Code:
    1. QFile file(PATH + filename);
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return;
    4. QDataStream in(&file);
    5. in >> check_magic;
    6. if (check_magic != magic)
    7. return false;
    8.  
    9. in >> check_version;
    10. if (check_version != version)
    11. return false;
    12. in.setVersion(QDataStream::Qt_4_8);
    13. for (int i = o; i < data.size(); i++)
    14. {
    15. in >> data[i].a;
    16. ...
    17. in.skipRawData(1); // otherwise the newline makes it messy
    18. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QDataStream interprets newline as end of file

    Try using QDataStream::atEnd() instead of QFile.atEnd().

  3. #3
    Join Date
    Mar 2012
    Location
    Germany
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream interprets newline as end of file

    Still the same.
    After the first iteration it stops.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream interprets newline as end of file

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2012
    Location
    Germany
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream interprets newline as end of file

    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.
    Qt Code:
    1. for (int i = 0; i < foo.size(); i++) {
    2. temp_list.clear()
    3. in >> foo;
    4. temp_list.append(foo)
    5. ...
    6. ...
    7. RealFoo.append(temp_list)
    8. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. for (int i = 0; i < foo.size(); i++) {
    2. in >> temp_string;
    3. foos[i]->set_a(temp_string.toFoo());
    4. ...
    5. ...
    6. } // after all foos are done
    7.  
    8. RealFoo.load_foos(foos);
    To copy to clipboard, switch view to plain text mode 
    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.
    :: Arch Linux KDE 4.9

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream interprets newline as end of file

    But are you sure you should be using QDataStream at all?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2012
    Location
    Germany
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream interprets newline as end of file

    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.
    :: Arch Linux KDE 4.9

  8. #8
    Join Date
    Mar 2012
    Location
    Germany
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream interprets newline as end of file

    It's getting interesting.

    When I do;
    Qt Code:
    1. for (int i = 0; i < size; i++) {
    2. while(!in.atEnd()) {
    3. in >> temp;
    4. qDebug() << temp << endl;
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.
    :: Arch Linux KDE 4.9

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDataStream interprets newline as end of file

    Show us the whole function.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 6
    Last Post: 9th September 2011, 13:29
  2. Replies: 6
    Last Post: 24th November 2010, 18:59
  3. about newline
    By drizzlefsh in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2009, 18:21
  4. QDataStream-serialization not writting to file
    By john_god in forum Qt Programming
    Replies: 4
    Last Post: 1st August 2009, 12:27
  5. Replies: 1
    Last Post: 16th October 2006, 07:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.