Using Low-Level I/O Functions
IBM C and C++ Compilers supports both stream
and low-level I/O. The primary difference between the two types
of I/O is that low-level I/O leaves the responsibility of
buffering and formatting up to you.
In general, you should not mix input or output
from low-level I/O with that from stream I/O. The only way to
communicate between stream I/O and low-level I/O is by using the
fdopen or fileno functions.
The low-level I/O functions include:
| access |
dup2 |
isatty |
stat |
| chmod |
__eof |
lseek |
_tell |
| _chsize |
fdopen |
open |
umask |
| close |
_filelength |
read |
write |
| creat |
fileno |
_setmode |
|
| dup |
fstat |
_sopen |
|
When you use the low-level I/O functions, you
should be aware of the following:
- A handle is a value that identifies a
file. It is created by the system and used by low-level
I/O functions.
For OS/2, the handle returned by low-level I/O functions
like open (called the C handle) is the same as that
returned by DosOpen (called the API handle). As a result,
you can get a file handle using the low-level I/O
functions, and then use it with system APIs.
Consequently, you can pass handles between library
environments without restriction. If you acquire a handle
other than by using the IBM C and C++ Compilers library
functions open, creat, _sopen, or fileno, you must run
_setmode for that handle before you use it with other IBM
C and C++ Compilers library functions.
Portability Note: If you will be
compiling your programs with other compilers, do not
write code that depends on the file handles being the
same.
For Windows, the runtime maps the file handle so that the
C handle and API handle are different. You can map from C
handle to API handle using _get_osfhandle, and you can
map from API handle to C handle using _open_osfhandle.
- The default open-sharing mode is
SH_DENYWR. Use _sopen to obtain other sharing modes.
- Text mode deletes '\r' characters on input
and changes '\n' to '\r\n' on output.
- In a multithread environment, you must
ensure that two threads do not attempt to perform
low-level I/O operations on the same file at the same
time. You must make sure that one I/O process is
completed before another begins.
- If the file mode is text, the low-level
I/O functions treat the character 'x1a' in the following
ways:
- If it is detected in a nonseekable
file, 'x1a' is treated as end-of-file. In a seekable
file, it is treated as end-of-file only if it is the
last character.
- If a file opened as text with either
the O_APPEND or O_RDWR flags and 'x1a' is the last
character of the file, the last character of the file
is deleted.

Alphabetical Listing of IBM C and C++
Compilers Functions and Macros