Results 1 to 6 of 6

Thread: Is it a bug?

  1. #1

    Default Is it a bug?

    Sorry for the subject, I didnt know what else to write. I am doing some silly Qt4 example programs just to learn the framework. In this case the snippet of the program adds lines on a text file. The problem is that sometimes it skips a whole line and sometimes not. It seems to depend on the data to be written:

    linux #cat birthday.dat
    giovanni12/07/1969
    giovanni12/07/1969
    giovanni12/07/1969

    fabio16/12/1966

    fabio16/12/1966

    fabio16/12/1966
    giovanni12/07/1969
    giovanni12/07/1969
    giovanni12/07/1969

    fabio16/12/1966

    fabio16/12/1966

    Here are the relevant snippets of the code (they are very short, so please read them):

    // file birthday.h

    class Birthday
    {
    public:
    explicit Birthday( const QString& );
    bool append( const QString&, const QString& );
    private:
    Birthday() {}
    const QString baseName;
    };

    // file birthday.cpp

    bool Birthday::append( const QString& name, const QString& date )
    {
    QFile file( baseName );
    if( !file.open( QIODevice::Append | QIODevice::Text ) )
    return false;
    QPair<QString, QString> data = qMakePair( name, date );
    QDataStream out( &file );
    out << data << "\n";
    file.close();
    return true;
    }

    // file main.cpp

    int main( int argc, char *argv[] )
    {
    QApplication app( argc, argv );
    Birthday bDay( "birthday.dat" );
    if( argc > 2 )
    {
    QStringList args = app.arguments();
    if( args.at(1) == "-a" ) {
    if( bDay.append( args.at(2), args.at(3) ) )
    return 0;
    else
    return 1;
    }
    [skip]
    }

    This issue is driving me mad. Can either anyone explain or is it a bug?
    Thanks in advance to anyone who will reply.

    fabio

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Is it a bug?

    you are misusing those classes:

    you open the file in text mode and then write binary date with QDataStream...
    you have to decide what you want.

  3. #3

    Default Re: Is it a bug?

    You're right: NO textmode.
    Anyway the outcome doesn't change.

    fabio

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Is it a bug?

    and as your are writing binary files (not text anymore)...

    you don't use a text file viewer (less, editor, ...) to inspect the binary file you have written, right?

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is it a bug?

    You are writing a QPair to a file, and expecting it to write two strings. Sure, it's writing those strings to the file, but theres no specific overloaded operator for that (unless you have added them yourself), so it's probably adding other stuff too, such as length for example. Since these bytes may be < 32, 'cat' is not going to do much with them unless you stumble across something like 0x0A ('\n') which it does understand.

    What you need to do is use something that understands binary files, such as dump, as you'll probably find each line has an additional character in front of it.

    A better append would be: out << name << date << "\n";

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is it a bug?

    or just use QTextStream and write 2 QStrings in text mode. QDataStream is a serialization class which can serialize anything with a suitable operator <<, but the way it serializes objects can be different than you might think. When you write something with QDataStream you can read it with QDataStream. For example it could be storing QStrings in base64 encoding so you will find a mess in your file but you will get the right QStrings if you read the file with QDataStream.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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.