今天在传输文件的时候,发现运行后,文件是过来了,但又没完全过来,文件的大小只有几kb,而原文件几m.
后面发现write函数用错了,
1.write(const char *data),
Writes data from a zero-terminated string of 8-bit characters to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
大概意思就是写入以0结尾的字符串。
相当于:QIODevice::write(data, qstrlen(data));
而qstrlen:
Returns the number of characters that precede the terminating ‘\0’, or 0 if str is 0.
错的原因应该就是存在换行,暂时低层还不清楚
2.write(const char *data, qint64 maxSize)
Writes at most maxSize bytes of data from data to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
则表示输入max个字节(byte)的数组(比如一个字符就是1byte,如果是汉字,则是2byte),bit是位,byte是字节。
想要完全写入自己的内容,且内容较大时,最好用这个。