SLAU132V October   2004  – February 2020

 

  1.   1
    1.     2
    2.     3
    3.     4
    4.     5
    5.     6
  2.   7
    1.     8
    2.     9
    3.     10
    4.     11
    5.     12
  3.   13
    1.     14
    2.     15
    3.     16
      1.      17
      2.      18
      3.      19
      4.      20
      5.      21
      6.      22
      7.      23
      8.      24
      9.      25
      10.      26
      11.      27
      12.      28
    4.     29
      1.      30
      2.      31
    5.     32
      1.      33
      2.      34
        1.       35
      3.      36
      4.      37
      5.      38
      6.      39
      7.      40
      8.      41
      9.      42
      10.      43
    6.     44
    7.     45
      1.      46
      2.      47
    8.     48
    9.     49
    10.     50
    11.     51
      1.      52
      2.      53
    12.     54
      1.      55
    13.     56
    14.     57
  4.   58
    1.     59
    2.     60
    3.     61
      1.      62
    4.     63
      1.      64
      2.      65
    5.     66
    6.     67
      1.      68
      2.      69
    7.     70
      1.      71
        1.       72
        2.       73
        3.       74
        4.       75
        5.       76
        6.       77
        7.       78
        8.       79
      2.      80
      3.      81
      4.      82
    8.     83
      1.      84
        1.       85
        2.       86
      2.      87
        1.       88
        2.       89
        3.       90
    9.     91
    10.     92
    11.     93
      1.      94
      2.      95
    12.     96
    13.     97
      1.      98
      2.      99
      3.      100
      4.      101
      5.      102
      6.      103
      7.      104
      8.      105
      9.      106
      10.      107
      11.      108
      12.      109
      13.      110
  5.   111
    1.     112
      1.      113
      2.      114
      3.      115
    2.     116
      1.      117
      2.      118
    3.     119
      1.      120
        1.       121
          1.        122
        2.       123
        3.       124
      2.      125
      3.      126
      4.      127
      5.      128
      6.      129
      7.      130
      8.      131
      9.      132
        1.       133
  6.   134
    1.     135
      1.      136
    2.     137
    3.     138
    4.     139
    5.     140
    6.     141
      1.      142
    7.     143
    8.     144
      1.      145
      2.      146
      3.      147
        1.       148
        2.       149
      4.      150
        1.       151
    9.     152
    10.     153
    11.     154
    12.     155
      1.      156
      2.      157
      3.      158
      4.      159
      5.      160
      6.      161
        1.       162
        2.       163
        3.       164
        4.       165
      7.      166
      8.      167
        1.       168
        2.       169
        3.       170
      9.      171
      10.      172
      11.      173
      12.      174
      13.      175
      14.      176
      15.      177
      16.      178
      17.      179
      18.      180
      19.      181
      20.      182
      21.      183
      22.      184
      23.      185
      24.      186
      25.      187
      26.      188
      27.      189
      28.      190
      29.      191
      30.      192
        1.       193
        2.       194
        3.       195
      31.      196
      32.      197
    13.     198
    14.     199
    15.     200
    16.     201
      1.      202
      2.      203
      3.      204
    17.     205
      1.      206
      2.      207
      3.      208
      4.      209
      5.      210
    18.     211
  7.   212
    1.     213
      1.      214
      2.      215
      3.      216
      4.      217
      5.      218
      6.      219
    2.     220
      1.      221
        1.       222
        2.       223
        3.       224
        4.       225
      2.      226
    3.     227
    4.     228
      1.      229
      2.      230
      3.      231
    5.     232
    6.     233
      1.      234
      2.      235
        1.       236
        2.       237
      3.      238
        1.       239
          1.        240
          2.        241
        2.       242
          1.        243
          2.        244
      4.      245
      5.      246
    7.     247
      1.      248
      2.      249
      3.      250
      4.      251
      5.      252
    8.     253
      1.      254
      2.      255
      3.      256
      4.      257
        1.       258
          1.        259
        2.       260
          1.        261
    9.     262
      1.      263
      2.      264
      3.      265
        1.       266
        2.       267
        3.       268
        4.       269
          1.        270
          2.        271
          3.        272
          4.        273
        5.       274
        6.       275
      4.      276
        1.       277
        2.       278
    10.     279
  8.   280
    1.     281
      1.      282
      2.      283
      3.      284
      4.      285
      5.      286
      6.      287
      7.      288
      8.      289
    2.     290
      1.      291
        1.       292
      2.      293
      3.      294
      4.      295
        1.       296
      5.      297
        1.       298
    3.     299
    4.     300
      1.      301
      2.      302
        1.       303
        2.       304
          1.        305
          2.        306
          3.        307
          4.        308
      3.      309
        1.       310
        2.       311
  9.   312
    1.     313
    2.     314
      1.      315
      2.      316
      3.      317
  10.   318
    1.     319
  11.   320
    1.     321

Overview of Low-Level I/O Implementation

The low-level functions are comprised of seven basic I/O functions: open, read, write, close, lseek, rename, and unlink. These low-level routines provide the interface between the high-level functions and the device-level drivers that actually perform the I/O command on the specified device.

The low-level functions are designed to be appropriate for all I/O methods, even those which are not actually disk files. Abstractly, all I/O channels can be treated as files, although some operations (such as lseek) may not be appropriate. See Section 7.2.3 for more details.

The low-level functions are inspired by, but not identical to, the POSIX functions of the same names.

The low-level functions operate on file descriptors. A file descriptor is an integer returned by open, representing an opened file. Multiple file descriptors may be associated with a file; each has its own independent file position indicator.

open
Open File for I/O
Syntax

#include <file.h>

int open (const char *path, unsigned flags, int file_descriptor);

Description

The open function opens the file specified by path and prepares it for I/O.

  • The path is the filename of the file to be opened, including an optional directory path and an optional device specifier (see Section 7.2.5).
  • The flags are attributes that specify how the file is manipulated. The flags are specified using the following symbols:
  • O_RDONLY (0x0000) /* open for reading */ O_WRONLY (0x0001) /* open for writing */ O_RDWR (0x0002) /* open for read & write */ O_APPEND (0x0008) /* append on each write */ O_CREAT (0x0200) /* open with file create */ O_TRUNC (0x0400) /* open with truncation */ O_BINARY (0x8000) /* open in binary mode */

    Low-level I/O routines allow or disallow some operations depending on the flags used when the file was opened. Some flags may not be meaningful for some devices, depending on how the device implements files.

  • The file_descriptor is assigned by open to an opened file.
  • The next available file descriptor is assigned to each new file opened.

Return Value

The function returns one of the following values:

non-negative file descriptor if successful
-1 on failure
close
Close File for I/O
Syntax

#include <file.h>

int close (int file_descriptor);

Description

The close function closes the file associated with file_descriptor.

The file_descriptor is the number assigned by open to an opened file.

Return Value

The return value is one of the following:

0 if successful
-1 on failure
read
Read Characters from a File
Syntax

#include <file.h>

int read (intfile_descriptor, char *buffer, unsignedcount);

Description

The read function reads count characters into the buffer from the file associated with file_descriptor.

  • The file_descriptor is the number assigned by open to an opened file.
  • The buffer is where the read characters are placed.
  • The count is the number of characters to read from the file.
Return Value

The function returns one of the following values:

0 if EOF was encountered before any characters were read
# number of characters read (may be less than count)
-1 on failure
write
Write Characters to a File
Syntax

#include <file.h>

int write (intfile_descriptor, const char *buffer, unsignedcount);

Description

The write function writes the number of characters specified by count from the buffer to the file associated with file_descriptor.

  • The file_descriptor is the number assigned by open to an opened file.
  • The buffer is where the characters to be written are located.
  • The count is the number of characters to write to the file.
Return Value

The function returns one of the following values:

# number of characters written if successful (may be less than count)
-1 on failure
lseek
Set File Position Indicator
Syntax for C

#include <file.h>

off_t lseek (intfile_descriptor, off_toffset, intorigin);

Description

The lseek function sets the file position indicator for the given file to a location relative to the specified origin. The file position indicator measures the position in characters from the beginning of the file.

  • The file_descriptor is the number assigned by open to an opened file.
  • The offset indicates the relative offset from the origin in characters.
  • The origin is used to indicate which of the base locations the offset is measured from. The origin must be one of the following macros:
  • SEEK_SET (0x0000) Beginning of file

    SEEK_CUR (0x0001) Current value of the file position indicator

    SEEK_END (0x0002) End of file

Return Value

The return value is one of the following:

# new value of the file position indicator if successful
(off_t)-1 on failure
Delete File
Syntax

#include <file.h>

int unlink (const char *path);

Description

The unlink function deletes the file specified by path. Depending on the device, a deleted file may still remain until all file descriptors which have been opened for that file have been closed. See Section 7.2.3.

The path is the filename of the file, including path information and optional device prefix. (See Section 7.2.5.)

Return Value

The function returns one of the following values:

0 if successful
-1 on failure
rename
Rename File
Syntax for C

#include {<stdio.h> | <file.h>}

int rename (const char *old_name, const char *new_name);

Syntax for C++

#include {<cstdio> | <file.h>}

int std::rename (const char *old_name, const char *new_name);

Description

The rename function changes the name of a file.

  • The old_name is the current name of the file.
  • The new_name is the new name for the file.
  • NOTE

    The optional device specified in the new name must match the device of the old name. If they do not match, a file copy would be required to perform the rename, and rename is not capable of this action.

Return Value

The function returns one of the following values:

0 if successful
-1 on failure

NOTE

Although rename is a low-level function, it is defined by the C standard and can be used by portable applications.