| Classes - Annotated - Tree - Functions - Home - Structure

# QIODevice Class Reference

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

`#include <qiodevice.h>`

Inherited by QBuffer, QFile, QSocket and QSocketDevice.

List of all member functions.

## Public Members

- QIODevice ()
- virtual ~QIODevice ()
- int flags () const
- int mode () const
- int state () const
- bool isDirectAccess () const
- bool isSequentialAccess () const
- bool isCombinedAccess () const
- bool isBuffered () const
- bool isRaw () const
- bool isSynchronous () const
- bool isAsynchronous () const
- bool isTranslated () const
- bool isReadable () const
- bool isWritable () const
- bool isReadWrite () const
- bool isInactive () const
- bool isOpen () const
- int status () const
- void resetStatus ()
- virtual bool open ( int mode )
- virtual void close ()
- virtual void flush ()
- virtual uint size () const
- virtual int at () const
- virtual bool at ( int )
- virtual bool atEnd () const
- bool reset ()
- virtual int readBlock ( char * data, uint maxlen )
- virtual int writeBlock ( const char * data, uint len )
- virtual int readLine ( char * data, uint maxlen )
- int writeBlock ( const QByteArray & data )
- QByteArray readAll ()
- virtual int getch ()
- virtual int putch ( int )
- virtual int ungetch ( int )

## Protected Members

- void setFlags ( int f ) (internal)
- void setType ( int ) (internal)
- void setMode ( int ) (internal)
- void setState ( int ) (internal)
- void setStatus ( int ) (internal)

## 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 superclass of all such devices; classes like QFile, QBuffer and QSocket inherit QIODevice and implement virtual functions like write() appropriately.

While applications sometimes use QIODevice directly, mostly it is better to go through QTextStream and QDataStream, which provide stream operations on any QIODevice subclass. QTextStream provides text-oriented stream functionality (for human-readable ASCII files, for example), while 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 argument to open().
- close() closes the device and tidies up.
- 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, if there is one for this device, or it moves the pointer.
- atEnd() says whether there is more to read, if that is a meaningful question 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) while 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 while 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 there and then, for example files. When you read from a file, the file delivers its data right away. Others, 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() tells 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 that. 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().)
- Accessibility. Some files cannot be written, for example. isReadable(), isWritable and isReadWrite() tells 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. This can quite obviously be set using open() :)

QIODevice provides numerous pure virtual functions you need to implement when subclassing it. Here is a skeleton subclass with all the members you are certain to need, and some it's likely that you will need:

        class YourDevice : public QIODevice
        {
        public:
            YourDevice();
           ~YourDevice();

            bool open( int mode );
            void close();
            void flush();

            uint size() const;
            int  at() const;        // not a pure virtual function
            bool at( int );         // not a pure virtual function
            bool atEnd() const;     // not a pure virtual function

            int readBlock( char *data, uint maxlen );
            int writeBlock( const char *data, uint len );
            int readLine( char *data, uint maxlen );

            int getch();
            int putch( int );
            int ungetch( int );
        };

The three non-pure virtual functions can be ignored if your device is sequential (e.g. an RS-232 port).

See also QDataStream and QTextStream.

## Member Function Documentation

### QIODevice::QIODevice ()

Constructs an I/O device.

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

Destructs the I/O device.

### bool QIODevice::at ( int pos ) `[virtual]`

Virtual function that sets the I/O device index to pos.

See also size().

Reimplemented in QBuffer, QSocketDevice, QSocket and QFile.

### int QIODevice::at () const `[virtual]`

Virtual function that returns the current I/O device index.

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

See also size().

Reimplemented in QBuffer, QFile, QSocket and QSocketDevice.

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

Virtual function that returns TRUE if the I/O device index is at the end of the input.

Reimplemented in QFile, QSocket and QSocketDevice.

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

Closes the I/O device.

This virtual function must be reimplemented by all subclasses.

See also open().

Reimplemented in QBuffer, QSocketDevice, QFile and QSocket.

### int QIODevice::flags () const

Returns the current I/O device flags setting.

Flags consists of mode flags and state flags.

See also mode() and state().

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

Flushes an open I/O device.

This virtual function must be reimplemented by all subclasses.

Reimplemented in QFile, QSocket, QBuffer and QSocketDevice.

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

Reads a single byte/character from the I/O device.

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

This virtual function must be reimplemented by all subclasses.

See also putch() and ungetch().

Reimplemented in QSocketDevice, QFile, QBuffer and QSocket.

### bool QIODevice::isAsynchronous () const

Returns TRUE if the I/O device is a asynchronous device, otherwise FALSE.

This mode is currently not in use.

See also isSynchronous().

### bool QIODevice::isBuffered () const

Returns TRUE if the I/O device is a buffered (not raw) device, otherwise FALSE.

See also isRaw().

### bool QIODevice::isCombinedAccess () const

Returns TRUE if the I/O device is a combined access (both direct and sequential) device, otherwise FALSE.

This access method is currently not in use.

### bool QIODevice::isDirectAccess () const

Returns TRUE if the I/O device is a direct access (not sequential) device, otherwise FALSE.

See also isSequentialAccess().

### bool QIODevice::isInactive () const

Returns TRUE if the I/O device state is 0, i.e. the device is not open.

See also isOpen().

### bool QIODevice::isOpen () const

Returns TRUE if the I/O device state has been opened, otherwise FALSE.

See also isInactive().

### bool QIODevice::isRaw () const

Returns TRUE if the I/O device is a raw (not buffered) device, otherwise FALSE.

See also isBuffered().

### bool QIODevice::isReadWrite () const

Returns TRUE if the I/O device was opened using `IO_ReadWrite` mode.

See also isReadable() and isWritable().

### bool QIODevice::isReadable () const

Returns TRUE if the I/O device was opened using `IO_ReadOnly` or `IO_ReadWrite` mode.

See also isWritable() and isReadWrite().

### bool QIODevice::isSequentialAccess () const

Returns TRUE if the I/O device is a sequential access (not direct) device, otherwise FALSE. Operations involving size() and at(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 FALSE.

See also isAsynchronous().

### bool QIODevice::isTranslated () const

Returns TRUE if the I/O device translates carriage-return and linefeed characters.

A QFile is translated if it is opened with the `IO_Translate` mode flag.

### bool QIODevice::isWritable () const

Returns TRUE if the I/O device was opened using `IO_WriteOnly` or `IO_ReadWrite` mode.

See also isReadable() and isReadWrite().

### int QIODevice::mode () const

Returns bits OR'ed together that specify the current operation mode.

These are the flags that were given to the open() function.

The flags are: `IO_ReadOnly, IO_WriteOnly, IO_ReadWrite, IO_Append, IO_Truncate` and `IO_Translate.`

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

Opens the I/O device using the specified mode. Returns TRUE if successful, or FALSE if the device could not be opened.

The mode parameter m must be a combination of the following flags.

- `IO_Raw` specified raw (unbuffered) file access.
- `IO_ReadOnly` opens a file in read-only mode.
- `IO_WriteOnly` opens a file in write-only mode.
- `IO_ReadWrite` opens a file in read/write mode.
- `IO_Append` sets the file index to the end of the file.
- `IO_Truncate` truncates the file.
- `IO_Translate` enables carriage returns and linefeed translation for text files under MS-DOS, Window, OS/2 and Macintosh. 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 when writing binary data to it. Cannot be combined with `IO_Raw.`

This virtual function must be reimplemented by all subclasses.

See also close().

Reimplemented in QBuffer, QFile, QSocketDevice and QSocket.

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

Writes the character ch to the I/O device.

Returns ch, or -1 if some error occurred.

This virtual function must be reimplemented by all subclasses.

See also getch() and ungetch().

Reimplemented in QSocket, QBuffer, QSocketDevice and QFile.

### QByteArray QIODevice::readAll ()

This convenience function returns all of the remaining data in the device. Note that this only works for direct access devices, such as QFile.

See also isDirectAccess().

### int QIODevice::readBlock ( char * data, uint maxlen ) `[virtual]`

Reads at most maxlen bytes from the I/O device into data and returns the number of bytes actually read.

This virtual function must be reimplemented by all subclasses.

See also writeBlock().

Reimplemented in QBuffer, QSocket, QSocketDevice and QFile.

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

Reads a line of text, up to maxlen bytes including a terminating \0. If there is a newline at the end if the line, it is not stripped.

Returns the number of bytes read, or -1 in case of error.

This virtual function can be reimplemented much more efficiently by the most subclasses.

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

Reimplemented in QSocket, QBuffer and QFile.

### bool QIODevice::reset ()

Sets the device index to 0.

See also at().

### void QIODevice::resetStatus ()

Sets the I/O device status to `IO_Ok.`

See also status().

### uint QIODevice::size () const `[virtual]`

Virtual function that returns the size of the I/O device.

See also at().

Reimplemented in QSocket, QFile, QBuffer and QSocketDevice.

### int QIODevice::state () const

Returns bits OR'ed together that specify the current state.

The flags are: `IO_Open.`

Subclasses may define more flags.

### int QIODevice::status () const

Returns the I/O device status.

The I/O device status returns an error code. If open() returns FALSE or readBlock() or writeBlock() return -1, this function can be called to get the reason why the operation did not succeed.

The status codes are:

- `IO_Ok` The operation was successful.
- `IO_ReadError` Could not read from the device.
- `IO_WriteError` Could not write to the device.
- `IO_FatalError` A fatal unrecoverable error occurred.
- `IO_OpenError` Could not open the device.
- `IO_ConnectError` Could not connect to the device.
- `IO_AbortError` The operation was unexpectedly aborted.
- `IO_TimeOutError` The operation timed out.
- `IO_OnCloseError` An unspecified error happened on close.

See also resetStatus().

### int QIODevice::ungetch ( int ch ) `[virtual]`

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

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

Returns ch, or -1 if some error occurred.

This virtual function must be reimplemented by all subclasses.

See also getch() and putch().

Reimplemented in QBuffer, QFile, QSocketDevice and QSocket.

### int QIODevice::writeBlock ( const QByteArray & data )

This convenience function is the same as calling writeBlock( data.data(), data.size() ).

### int QIODevice::writeBlock ( const char * data, uint len ) `[virtual]`

Writes len bytes from p to the I/O device and returns the number of bytes actually written.

This virtual function must be reimplemented by all subclasses.

See also readBlock().

Reimplemented in QBuffer, QSocketDevice, QFile and QSocket.

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

For internal use only.

### void QIODevice::setMode ( int m ) `[protected]`

For internal use only.

### void QIODevice::setState ( int s ) `[protected]`

For internal use only.

### void QIODevice::setStatus ( int s ) `[protected]`

For internal use only.

### void QIODevice::setType ( int t ) `[protected]`

For internal use only.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.