Qt模組化筆記之core——Input/Output檔案操作-流QTextStream使用
阿新 • • 發佈:2019-02-10
QTextStream是與c++的iostream相似的流類,用於更加方便地操作檔案文字。
那麼,什麼是流呢?
流可以理解為資料通過“管道”轉移,這是就資料流了。其實將流理解為管道與流體的結合體更好,當我們需要操作某個檔案時,將管道的一端接到這個檔案上,如這個類的函式:QTextStream(FILE * fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite),傳入檔案控制代碼fileHandle(可以是一個QFile物件)。我們也可以指定流的流向,使用第二個引數:QIODevice::OpenMode控制它,如只讀QIODevice::Readonly或只寫QIODevice::Writeonly等。這樣,對檔案的讀寫就抽象為資料流的流動了。
QTextStream公有函式如下:
列舉值:QTextStream() QTextStream(QIODevice * device) QTextStream(FILE * fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite) QTextStream(QString * string, QIODevice::OpenMode openMode = QIODevice::ReadWrite) QTextStream(QByteArray * array, QIODevice::OpenMode openMode = QIODevice::ReadWrite) QTextStream(const QByteArray & array, QIODevice::OpenMode openMode = QIODevice::ReadOnly) virtual ~QTextStream() void setAutoDetectUnicode(bool enabled)//自動查詢編碼標識:BOM(byte-order mark),即位元組順序標記,它是插入到以UTF-8、UTF16或UTF-32編碼Unicode檔案開頭的特殊標記,用來識別Unicode檔案的編碼型別。 void setCodec(QTextCodec * codec)//設定編碼,預設是QTextCodec::codecForLocale(),即電腦預設的win為gbk,linux為utf-8 void setCodec(const char * codecName)//字元形式編碼設定,例:setCodec("UTF-8"); void setGenerateByteOrderMark(bool generate)//當utf編碼被使用,BOM資訊會被寫入到檔案中 void setIntegerBase(int base)//設定讀取整數時採取的進位制 void setNumberFlags(NumberFlags flags)//影響輸出數字的顯示格式,有顯示基數(QTextStream::ShowBase)如:16 則有("0x"), 8 有("0")等,列舉值1. void setFieldWidth(int width)//如果設定了域寬FieldWidth,再設定域中字元對齊方式FieldAlignment(下一個函式),並設定填充字元PadChar,則一串字元將佔據FieldWidth寬度,不足部分用PadChar填充,並在其中對齊。如"***hi"即FieldWidth為5,FieldAlignment為right,PadChar為*的輸出結果。 void setFieldAlignment(FieldAlignment mode)//列舉值2 void setPadChar(QChar ch) void setRealNumberNotation(RealNumberNotation notation)//實數的記數方式,如顯示為科學記數法等。列舉值3 void setRealNumberPrecision(int precision)//設定實數精度,即小數位數 void setStatus(Status status)//返回流的狀態,似乎這個設定函式沒什麼實用性,列舉值4 void setDevice(QIODevice * device) void setString(QString * string, QIODevice::OpenMode openMode = QIODevice::ReadWrite) void setLocale(const QLocale & locale) QString read(qint64 maxlen) QString readAll() QString readLine(qint64 maxlen = 0) void reset() void resetStatus() bool seek(qint64 pos) void flush() void skipWhiteSpace()//跳過空字元,當一個一個字元讀取時有用 bool atEnd() const bool autoDetectUnicode() const QTextCodec * codec() const QIODevice * device() const FieldAlignment fieldAlignment() const int fieldWidth() const bool generateByteOrderMark() const int integerBase() const QLocale locale() const NumberFlags numberFlags() const QChar padChar() const qint64 pos() const Status status() const QString * string() const RealNumberNotation realNumberNotation() const int realNumberPrecision() const QTextStream & operator<<(QString & str)//流的運算子能操縱各種資料型別,這裡弄出來兩個代表,<<為向流中插入,>>從流中讀取。 QTextStream & operator>>(QString & str)// ………………
1,enum QTextStream::NumberFlag
Constant | Value | Description |
---|---|---|
QTextStream::ShowBase | 0x1 | 如果進位制是16 ("0x"), 8 ("0"), 或2 ("0b").將進位制顯示相相應前輟 |
QTextStream::ForcePoint | 0x2 | 在數字中總顯示小數點 |
QTextStream::ForceSign | 0x4 | Always put the sign in numbers, even for positive numbers. |
QTextStream::UppercaseBase | 0x8 | Use uppercase versions of base prefixes ("0X", "0B"). |
QTextStream::UppercaseDigits | 0x10 |
Use uppercase letters for expressing digits 10 to 35 instead of lowercase. |
Constant | Value | Description |
---|---|---|
QTextStream::AlignLeft | 0 | Pad on the right side of fields. |
QTextStream::AlignRight | 1 | Pad on the left side of fields. |
QTextStream::AlignCenter | 2 | Pad on both sides of field. |
QTextStream::AlignAccountingStyle | 3 | Same as AlignRight, except that the sign of a number is flush left. |
Constant | Value | Description |
---|---|---|
QTextStream::ScientificNotation | 2 | Scientific notation (printf()'s %e flag). |
QTextStream::FixedNotation | 1 | Fixed-point notation (printf()'s %f flag). |
QTextStream::SmartNotation | 0 | Scientific or fixed-point notation, depending on which makes most sense (printf()'s %g flag). |
Constant | Value | Description |
---|---|---|
QTextStream::Ok | 0 | The text stream is operating normally. |
QTextStream::ReadPastEnd | 1 | The text stream has read past the end of the data in the underlying device. |
QTextStream::ReadCorruptData | 2 | The text stream has read corrupt data. |
QTextStream::WriteFailed | 3 | The text stream cannot write to the underlying device. |
與iostream一樣,它有很多操縱符,這些操縱符能在輸入輸出中直接使用,用於格式化輸出
Manipulator | Description |
---|---|
bin | Same as setIntegerBase(2).設定進位製為2 |
oct | Same as setIntegerBase(8). |
dec | Same as setIntegerBase(10). |
hex | Same as setIntegerBase(16). |
showbase | |
forcesign | |
forcepoint | |
noshowbase | |
noforcesign | |
noforcepoint | |
uppercasebase | |
uppercasedigits | |
lowercasebase | |
lowercasedigits | |
fixed | Same as setRealNumberNotation(FixedNotation). |
scientific | Same as setRealNumberNotation(ScientificNotation). |
left | Same as setFieldAlignment(AlignLeft). |
right | Same as setFieldAlignment(AlignRight). |
center | Same as setFieldAlignment(AlignCenter). |
endl | Same as operator<<('\n') and flush(). |
flush | Same as flush(). |
reset | Same as reset(). |
bom | Same as setGenerateByteOrderMark(true). |
從官網抄來的典型使用方法:
QFile data("output.txt");
if (data.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&data);
out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;//qSetFieldWidth、left 為上述列表中的操縱符
// writes "Result: 3.14 2.7 ",從3.14到2.7的前面正好10個字元,左對齊,預設以空白字元填空
}
也可以改成如下:
QTextStream out(&data);
out.setFieldWidth(10);
out.setFieldAlignment(QTextStream::AlignLeft);
out.setPadChar("*");
out<<3.14<<2.7;
結果將如下:
3.14******2.7*******。