PDA

View Full Version : Is it a bug?



TheQuest
12th October 2009, 16:46
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

caduel
12th October 2009, 16:59
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.

TheQuest
12th October 2009, 17:07
You're right: NO textmode.
Anyway the outcome doesn't change.

fabio

caduel
12th October 2009, 17:39
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?

squidge
12th October 2009, 18:29
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";

faldzip
12th October 2009, 20:42
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.