Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

readRawData



  • I am using the readRawData to read a file as follows:

    char *date1 = new char[10];
    in.readRawData(date1,10);
    qDebug() << "date"<<date1;
    

    but despite the specified length of 10 chars, I've got the following result with QDebug:

    date 1807120942??????
    

    I am wandering why these question marks after 10 chars?

    The view from the Debugger is shown below:

    	date1	"1807120942������"	char*
    			'1' 	49    	0x31	char
    			'8' 	56    	0x38	char
    			'0' 	48    	0x30	char
    			'7' 	55    	0x37	char
    			'1' 	49    	0x31	char
    			'2' 	50    	0x32	char
    			'0' 	48    	0x30	char
    			'9' 	57    	0x39	char
    			'4' 	52    	0x34	char
    			'2' 	50    	0x32	char
    			'○' 	-18/238	0xee	char
    			    	-2/254	0xfe	char
    			'○' 	-18/238	0xee	char
    			    	-2/254	0xfe	char
    			'○' 	-18/238	0xee	char
    			    	-2/254	0xfe	char


  • qDebug() with char* uses std::strlen to determine the lenght of the string. If you don't null-terminate it it will continue reading your memory until it finds a byte set at 0

    char *date1 = new char[11];
    date1[10]='\0';
    in.readRawData(date1,10);
    qDebug() << "date"<<date1;
    

    Or using Qt's convenient QByteArray

    QByteArray date1(10,0);
    in.readRawData(date1.data(),10);
    qDebug() << "date"<<date1;
    


  • Thanks VRonin for your quck answer, it's now working perfectly !

    I am wandering what are the two arguments in the QByteArray definition:
    (can't find it in the doc !!)

    QByteArray date1(10,0);

    I guess 10 is the size of the array but what is the zero standing for?

    Thks again for your help


  • Moderators

    @jipe3001
    reading the decumentation is helpful, Qt, has one of the best out there.

    link QByteArray


Log in to reply