Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QIODevice Class Reference

The QIODevice class is the base class of I/O devices. More...

    #include <QIODevice>

Inherited by QSocket, QSocketDevice, QBuffer, QFile, and QAbstractSocket.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members
- Compatibility members

### Public Types

- typedef Offset
- enum OpenMode { ReadOnly, WriteOnly, ReadWrite, Append, Truncate, Translate }

### Public Functions

- QIODevice ()
- virtual ~QIODevice ()
- virtual Q_LONGLONG at () const = 0
- virtual bool atEnd () const
- virtual void close () = 0
- QString errorString () const
- int flags () const
- virtual void flush ()
- virtual int getch ()
- bool isAsynchronous () const
- bool isBuffered () const
- bool isCombinedAccess () const
- bool isDirectAccess () const
- bool isInactive () const
- virtual bool isOpen () const = 0
- bool isRaw () const
- bool isReadWrite () const
- bool isReadable () const
- bool isSequentialAccess () const
- bool isSynchronous () const
- bool isTranslated () const
- bool isWritable () const
- int mode () const
- virtual bool open ( int mode )
- virtual int putch ( int character )
- virtual Q_LONGLONG read ( char * data, Q_LONGLONG len ) = 0
- QByteArray read ( Q_LONGLONG maxlen )
- virtual QByteArray readAll ()
- virtual Q_LONGLONG readLine ( char * data, Q_LONGLONG maxlen )
- virtual QByteArray readLine ()
- bool reset ()
- void resetStatus ()
- virtual bool seek ( Q_LONGLONG offset ) = 0
- virtual Q_LONGLONG size () const = 0
- int status () const
- virtual int ungetch ( int character ) = 0
- virtual Q_LONGLONG write ( const char * data, Q_LONGLONG len ) = 0
- Q_LONGLONG write ( const QByteArray & data )

### Protected Functions

- void setBufferSize ( int )
- void setFlags ( int flags )

## Detailed Description

The QIODevice class is the base class of I/O devices.

An I/O device represents a medium that one can read bytes from and/or write bytes to. The QIODevice class is the abstract super-class of all such devices; classes such as QFile, QBuffer and QSocket inherit QIODevice, and implement virtual functions such as write() appropriately.

Although applications sometimes use QIODevice directly, it is usually better to use QTextStream and QDataStream which provide stream operations on any QIODevice subclass. QTextStream provides text-oriented stream functionality (for human-readable ASCII files, for example), whereas QDataStream deals with binary data in a totally platform-independent manner.

The public member functions in QIODevice roughly fall into two groups: the action functions and the state access functions. The most important action functions are:

- open() opens a device for reading and/or writing, depending on the mode argument.
- close() closes the device and tidies up (e.g. flushes buffered data).
- readBlock() reads a block of data from the device.
- writeBlock() writes a block of data to the device.
- readLine() reads a line (of text, usually) from the device.
- flush() ensures that all buffered data are written to the real device.

There are also some other, less used, action functions:

- getch() reads a single character.
- ungetch() forgets the last call to getch(), if possible.
- putch() writes a single character.
- size() returns the size of the device, if there is one.
- at() returns the current read/write pointer's position, if there is one for this device, or it moves the pointer if given an offset.
- atEnd() indicates whether there is more to read, if this is meaningful for this device.
- reset() moves the read/write pointer to the start of the device, if that is possible for this device.

The state access are all "get" functions. The QIODevice subclass calls setState() to update the state, and simple access functions tell the user of the device what the device's state is. Here are the settings, and their associated access functions:

- Access type. Some devices are direct access (it is possible to read/write anywhere), whereas others are sequential. QIODevice provides the access functions (isDirectAccess(), isSequentialAccess(), and isCombinedAccess()) to tell users what a given I/O device supports.
- Buffering. Some devices are accessed in raw mode, whereas others are buffered. Buffering usually provides greater efficiency, particularly for small read/write operations. isBuffered() tells the user whether a given device is buffered. (This can often be set by the application in the call to open().)
- Synchronicity. Synchronous devices work immediately (for example, files). When you read from a file, the file delivers its data straight away. Other kinds of device, such as a socket connected to a HTTP server, may not deliver the data until seconds after you ask to read it. isSynchronous() and isAsynchronous() tell the user how this device operates.
- CR/LF translation. For simplicity, applications often like to see just a single CR/LF style, and QIODevice subclasses can provide this. isTranslated() returns true if this object translates CR/LF to just LF. (This can often be set by the application in the call to open().)
- Permissions. Some files cannot be written. For example, isReadable(), isWritable(), and isReadWrite() tell the application whether it can read from and write to a given device. (This can often be set by the application in the call to open().)
- Finally, isOpen() returns true if the device is open; i.e. after an open() call.

See also QDataStream and QTextStream.

## Member Type Documentation

### typedef QIODevice::Offset

The offset (device position) within the device.

### enum QIODevice::OpenMode

`QIODevice::ReadOnly` | The device can only be read from.
`QIODevice::WriteOnly` | The device can only be written to.
`QIODevice::ReadWrite` | The device can be both read from and written to.
`QIODevice::Append` | Data written to the device is appended to the end of existing data.
`QIODevice::Truncate` |
`QIODevice::Translate` | Translate line ending conventions.

## Member Function Documentation

### QIODevice::QIODevice ()

Constructs an I/O device.

### QIODevice::~QIODevice () `[virtual]`

Destroys the I/O device.

### Q_LONGLONG QIODevice::at () const `[pure virtual]`

Returns the current I/O device position.

This is the position of the data read/write head of the I/O device.

This virtual function must be reimplemented by all subclasses.

See also size().

### bool QIODevice::atEnd () const `[virtual]`

Returns true if the I/O device position is at the end of the input; otherwise returns false.

See also at() and size().

### void QIODevice::close () `[pure virtual]`

Closes the I/O device.

This virtual function must be reimplemented by all subclasses.

See also open().

### QString QIODevice::errorString () const

Returns a human-readable description of an error that occurred on the device. The error described by the string corresponds to changes of QIODevice::status(). If the status is reset, the error string is also reset.

        QFile file("address.dat");
        if (!file.open(QIODevice::ReadOnly) {
            QMessageBox::critical(this, tr("Error"),
                    tr("Could not open file for reading: %1")
                    .arg(file.errorString()));
            return;
        }

See also resetStatus().

### int QIODevice::flags () const

Returns the current I/O device flags setting.

The flags value is a logical OR of the mode flags and state flags.

See also mode() and state().

### void QIODevice::flush () `[virtual]`

Flushes the open I/O device.

### int QIODevice::getch () `[virtual]`

Reads a single character from the I/O device.

Returns the character read, or -1 if the end of the I/O device has been reached.

See also putch() and ungetch().

### bool QIODevice::isAsynchronous () const

Returns true if the device is an asynchronous device; returns false if the device is a synchronous device.

This mode is currently not in use.

See also isSynchronous().

### bool QIODevice::isBuffered () const

Returns true if the I/O device is a buffered device; returns false if the device is a raw device.

See also isRaw().

### bool QIODevice::isCombinedAccess () const

Returns true if the I/O device is a combined access (both direct and sequential) device; otherwise returns false.

This access method is currently not in use.

### bool QIODevice::isDirectAccess () const

Returns true if the I/O device is a direct access device; returns false if the device is a sequential access device.

See also isSequentialAccess().

### bool QIODevice::isInactive () const

Returns true if the I/O device state is 0, i.e. the device is not open; otherwise returns false.

See also isOpen().

### bool QIODevice::isOpen () const `[pure virtual]`

Returns true if the I/O device has been opened; otherwise returns false.

See also isInactive().

### bool QIODevice::isRaw () const

Returns true if the device is a raw device; otherwise returns false if the device is a buffered device.

See also isBuffered().

### bool QIODevice::isReadWrite () const

Returns true if the I/O device was opened in `QIODevice::ReadWrite` mode; otherwise returns false.

See also isReadable() and isWritable().

### bool QIODevice::isReadable () const

Returns true if the I/O device was opened in `QIODevice::ReadOnly` or `QIODevice::ReadWrite` mode; otherwise returns false.

See also isWritable() and isReadWrite().

### bool QIODevice::isSequentialAccess () const

Returns true if the device is a sequential access device; returns false if the device is a direct access device.

Operations involving size() and seek(int) are not valid on sequential devices.

See also isDirectAccess().

### bool QIODevice::isSynchronous () const

Returns true if the I/O device is a synchronous device; otherwise returns false.

See also isAsynchronous().

### bool QIODevice::isTranslated () const

Returns true if the I/O device translates carriage return and linefeed characters; otherwise returns false.

A QFile is translated if it is opened with the `QIODevice::Translate` mode flag.

### bool QIODevice::isWritable () const

Returns true if the I/O device was opened in `QIODevice::WriteOnly` or `QIODevice::ReadWrite` mode; otherwise returns false.

See also isReadable() and isReadWrite().

### int QIODevice::mode () const

Returns a value specifying the current operation mode. This is a selection of mode flags, combined using the logical OR operator.

See also OpenMode.

### bool QIODevice::open ( int mode ) `[virtual]`

Opens the I/O device in the specified mode. Returns true if the device was successfully opened; otherwise returns false.

The mode parameter mode must be selection of the following flags, combined using the OR operator:

Mode flags | Meaning
--- | ---
`QIODevice::Raw` | specifies raw (unbuffered) file access.
`QIODevice::ReadOnly` | opens a file in read-only mode.
`QIODevice::WriteOnly` | opens a file in write-only mode.
`QIODevice::ReadWrite` | opens a file in read/write mode.
`QIODevice::Append` | sets the file index to the end of the file.
`QIODevice::Truncate` | truncates the file.
`QIODevice::Translate` | enables carriage returns and linefeed translation for text files under MS-DOS, Windows, and Mac OS X. On Unix systems this flag has no effect. Use with caution as it will also transform every linefeed written to the file into a CRLF pair. This is likely to corrupt your file if you write write binary data. Cannot be combined with `QIODevice::Raw`.

Some devices make less sense to use this entry point to open, for example connecting to a server in QSocketDevice. In this case the default implementation will return false indicating that the open failed and another approach to put the file into isOpen() mode should be attempted.

### int QIODevice::putch ( int character ) `[virtual]`

Writes the character to the I/O device.

Returns the character, or -1 if an error occurred.

See also getch().

### Q_LONGLONG QIODevice::read ( char * data, Q_LONGLONG len ) `[pure virtual]`

Reads a number of characters from the I/O device into data. At most, the maximum number of characters will be read.

Returns -1 if a fatal error occurs, or 0 if there are no bytes to read.

The device must be opened for reading, and data must not be 0.

This virtual function must be reimplemented by all subclasses.

See also writeBlock(), isOpen(), and isReadable().

### QByteArray QIODevice::read ( Q_LONGLONG maxlen )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This convenience function will read data into a preallocated QByteArray.

### QByteArray QIODevice::readAll () `[virtual]`

This convenience function returns all of the remaining data in the device.

### Q_LONGLONG QIODevice::readLine ( char * data, Q_LONGLONG maxlen ) `[virtual]`

Reads characters from the device into data up to and including the next newline character ('\n') or until at most maxlen - 1 characters have been read. A terminating '\0' is appended to the characters stored in data.

Returns the total number of characters read, including the terminating '\0', if no error occurred; otherwise returns -1.

To ensure that your data buffer is large enough to contain the maximum number of characters read from the device, pass a value that is one character less than the size of your buffer.

Example:

        char buffer[12];
        device->readLine(buffer, sizeof(buffer));

The above code will read bytes from a device in the following way:

Input from the device | Buffer contents | Length returned | Comment
--- | --- | --- | ---
Trolltech\n | Trolltech\n\0 | 11 | 10 characters from the device plus a zero byte.
Hello world\n | Hello world\0 | 12 | 11 characters from the device plus a zero byte. Note that the newline character has not yet been read.
The cow jumped over the moon.\n | The cow jum\0 | 12 | 11 characters from the device plus a zero byte. The rest of the string has not yet been read.

See also read() and QTextStream::readLine().

### QByteArray QIODevice::readLine () `[virtual]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This convenience function will read a line and place it into a QByteArray.

See also QIODevice::readLine().

### bool QIODevice::reset ()

Sets the device position to 0.

See also at() and seek().

### void QIODevice::resetStatus ()

Sets the I/O device status to `QIODevice::Ok`.

See also status().

### bool QIODevice::seek ( Q_LONGLONG offset ) `[pure virtual]`

Sets the I/O device position to the offset given. Returns true if the position was successfully set (the offset is within range and the seek was successful); otherwise returns false.

If the device is sequential, the offset is relative to the current position.

This virtual function must be reimplemented by all subclasses.

See also size().

### void QIODevice::setBufferSize ( int ) `[protected]`

### void QIODevice::setFlags ( int flags ) `[protected]`

Used by subclasses to set the device flags.

See also OpenMode.

### Q_LONGLONG QIODevice::size () const `[pure virtual]`

Returns the size of the I/O device.

This virtual function must be reimplemented by all subclasses.

See also at().

### int QIODevice::status () const

Returns the I/O device status.

The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.

The status codes are:

Status code | Meaning
--- | ---
`QIODevice::Ok` | The operation was successful.
`QIODevice::ReadError` | Could not read from the device.
`QIODevice::WriteError` | Could not write to the device.
`QIODevice::FatalError` | A fatal unrecoverable error occurred.
`QIODevice::OpenError` | Could not open the device.
`QIODevice::ConnectError` | Could not connect to the device.
`QIODevice::AbortError` | The operation was unexpectedly aborted.
`QIODevice::TimeOutError` | The operation timed out.
`QIODevice::UnspecifiedError` | An unspecified error happened on close.

See also resetStatus().

### int QIODevice::ungetch ( int character ) `[pure virtual]`

Puts the character back into the I/O device, and decrements the index position if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns character, or -1 if an error occurred.

This virtual function must be reimplemented by all subclasses.

See also getch() and putch().

### Q_LONGLONG QIODevice::write ( const char * data, Q_LONGLONG len ) `[pure virtual]`

Writes a number of characters from data to the I/O device. At most, the maximum of characters will be written. If successful, the number of characters written is returned; otherwise -1 is returned.

This virtual function must be reimplemented by all subclasses.

See also read().

### Q_LONGLONG QIODevice::write ( const QByteArray & data )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This convenience function is the same as:

        write(data.data(), data.size());