JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Programming Interfaces Guide     Oracle Solaris 11 Information Library
search filter icon
search icon

Document Information

Preface

1.  Memory and CPU Management

2.  Remote Shared Memory API for Oracle Solaris Clusters

3.  Session Description Protocol API

4.  Process Scheduler

5.  Locality Group APIs

6.  Input/Output Interfaces

Files and I/O Interfaces

Basic File I/O

Advanced File I/O

File System Control

Using File and Record Locking

Choosing a Lock Type

Selecting Advisory or Mandatory Locking

Cautions About Mandatory Locking

Supported File Systems

Opening a File for Locking

Setting a File Lock

Setting and Removing Record Locks

Getting Lock Information

Process Forking and Locks

Deadlock Handling

Terminal I/O Functions

7.  Interprocess Communication

8.  Socket Interfaces

9.  Programming With XTI and TLI

10.  Packet Filtering Hooks

11.  Transport Selection and Name-to-Address Mapping

12.  Real-time Programming and Administration

13.  The Oracle Solaris ABI and ABI Tools

A.  UNIX Domain Sockets

Index

Files and I/O Interfaces

Files that are organized as a sequence of data are called regular files. Regular files can contain ASCII text, text in some other binary data encoding, executable code, or any combination of text, data, and code.

A regular file is made up of the following components:

The Oracle Solaris operating system provides the following basic forms of file input/output interfaces:

Basic File I/O

The following interfaces perform basic operations on files and on character I/O devices.

Table 6-1 Basic File I/O Interfaces

Interface Name
Purpose
Open a file for reading or writing
Close a file descriptor
Read from a file
Write to a file
Create a new file or rewrite an existing one
Remove a directory entry
Move read/write file pointer

The following code sample demonstrates the use of the basic file I/O interface. read(2) and write(2) both transfer no more than the specified number of bytes, starting at the current offset into the file. The number of bytes actually transferred is returned. The end of a file is indicated on a read(2) by a return value of zero.

Example 6-1 Basic File I/O Interface

#include            <fcntl.h>
#define            MAXSIZE            256

main()
{
    int     fd;
    ssize_t n;
    char        array[MAXSIZE];

    fd = open ("/etc/motd", O_RDONLY);
    if (fd == -1) {
        perror ("open");
        exit (1);
    }
    while ((n = read (fd, array, MAXSIZE)) > 0)
        if (write (1, array, n) != n)
            perror ("write");
    if (n == -1)
        perror ("read");
    close (fd);
}

When you are done reading or writing a file, always call close(2). Do not call close(2) for a file descriptor that was not returned from a call to open(2).

File pointer offsets into an open file are changed by using read(2), write(2), or by calls to lseek(2). The following example demonstrates the uses of lseek.

off_t     start, n;
struct    record    rec;

/* record current offset in start */
start = lseek (fd, 0L, SEEK_CUR);

/* go back to start */
n = lseek (fd, -start, SEEK_SET);
read (fd, &rec, sizeof (rec));

/* rewrite previous record */
n = lseek (fd, -sizeof (rec), SEEK_CUR);
write (fd, (char *&rec, sizeof (rec));

Advanced File I/O

The following table lists the tasks performed by advanced file I/O interfaces.

Table 6-2 Advanced File I/O Interfaces

Interface Name
Purpose
Link to a file
Determine accessibility of a file
Make a special or ordinary file
Change mode of file
Change owner and group of a file
Set file access and modification times
Get file status
Perform file control functions
Control device
Get configurable path name variables
Perform directory operations
Make a directory
Read the value of a symbolic link
Change the name of a file
Remove a directory
Make a symbolic link to a file

File System Control

The file system control interfaces listed in the following table enable the control of various aspects of the file system.

Table 6-3 File System Control Interfaces

Interface Name
Purpose
Get file system statistics
Update super block
Mount a file system
Get file system information
Get file system type information