文件和數據流函數

The QIODevice class is the base interface class of all I/O devices in Qt Core . QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data. The device can be a memory buffer, a file, or a datastream.

Some subclasses like QFile have been implemented using a memory buffer for intermediate storing of data. This speeds up programs by reducing read/write operations. Buffering makes functions like getChar () 和 putChar () fast, as they can operate on the memory buffer instead of directly on the device itself.

The QFile class provides functions for reading from and writing to files. A QFile 可以單獨使用,或更方便一起使用與 QTextStream or QDataStream .

QBuffer 允許訪問 QByteArray 使用 QIODevice 接口。 QByteArray is treated just as a standard random-accessed file. An example:

QBuffer buffer;
char ch;
buffer.open(QBuffer::ReadWrite);
buffer.write("Qt rocks!");
buffer.seek(0);
buffer.getChar(&ch);  // ch == 'Q'
buffer.getChar(&ch);  // ch == 't'
buffer.getChar(&ch);  // ch == ' '
buffer.getChar(&ch);  // ch == 'r'
					

調用 open () to open the buffer. Then call write () 或 putChar () to write to the buffer, and read (), readLine (), readAll (),或 getChar () to read from it. size () returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by calling seek (). When you are done with accessing the buffer, call close ().

The QDataStream 類提供把二進製數據序列化到 QIODevice . A data stream is a binary stream of encoded information which is 100% inde- pendent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris. You can also use a data stream to read/write raw unencoded binary data.

For more details on the datatypes that QDataStream can serialize, see 序列化 Qt 數據類型 .

The QTextStream class provides a convenient interface for reading and writing text. QTextStream can operate on a QIODevice QByteArray QString 。使用 QTextStream 's streaming operators, you can conveniently read and write words, lines and numbers. It's also common to use QTextStream to read console input and write console output.

There are three general ways to use QTextStream when reading text files:

  • 逐個分塊,通過調用 readLine () 或 readAll ().
  • Word by word. QTextStream supports streaming into QString s, QByteArray 及 char* 緩衝。單詞由空格定界,並自動跳過前導空白。
  • 逐個字符,通過流化成 QChar 或 char 類型。此方法經常用於方便處理輸入當剖析文件時,獨立於字符編碼和行尾語義。要跳過空白,調用 skipWhiteSpace ().

QByteArray 可以用於存儲原生字節 (包括 \0 ) 和傳統 8 位 \0 結尾字符串。使用 QByteArray is much more convenient than using const char *. It always ensures that the data is followed by a '\0' terminator, and uses 隱式共享類 (寫入時拷貝) 以縮減內存用量並避免不必要的數據拷貝。

除瞭 QByteArray ,Qt 還提供 QString 類以存儲字符串數據。對於大多數目的, QString is the most appropriate class to use. It stores 16-bit Unicode characters. It is, however, a good idea to use QByteArray when you need to store raw binary data, and when memory conservation is critical (for example, with Qt for Embedded Linux).