Go to main content

Oracle® Solaris 11.4 Programming Interfaces Guide

Exit Print View

Updated: November 2020
 
 

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:

  • Control data, which is called the inode. This data includes the file type, the access permissions, the owner, the file size, and the location of the data blocks.

  • File contents is a non-terminated sequence of bytes.

The Oracle Solaris OS provides the following basic forms of file I/O interfaces:

  • The basic file I/O interfaces are described in Basic File I/O.

  • The standard I/O buffering provides an easier interface and improved efficiency to run an application on a system without virtual memory. In an application running in a virtual memory environment, such as Oracle Solaris, standard file I/O is outdated.

  • The memory mapping interface is described in Memory Management Interfaces. Mapping files is the efficient form of the file I/O for applications running in the Oracle Solaris platform.

Basic File I/O

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

Table 1  Basic File I/O Interfaces
Interface Name
Purpose
open
Open a file for reading or writing. For more information, see the open(2) man page.
close
Close a file descriptor. For more information, see the close(2) man page.
read
Read from a file. For more information, see the read(2) man page.
write
Write to a file. For more information, see the write(2) man page.
creat
Create a new file or rewrite an existing one. For more information, see the creat(2) man page.
unlink
Remove a directory entry. For more information, see the unlink(2) man page.
lseek
Move read/write file pointer. For more information, see the lseek(2) man page.

The following code sample demonstrates the use of the basic file I/O interface.

Example 17  Using the 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);
}

In this example, the read and the write interfaces both transfer the specified number of bytes, starting at the current offset into the file. The number of bytes transferred is returned. The end of a file is indicated on a read by a return value of zero. For more information, see the read(2) and write(2) man pages.

When you are done reading or writing a file, call the close interface. Do not call close for a file descriptor that was not returned from a call to open. For more information, see the close(2), and open(2) man pages.

File pointer offsets into an open file are changed by using read, write, or by calls to lseek. For more information, see the read(2), write(2) and lseek(2) man pages.

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 2  Advanced File I/O Interfaces
Interface Name
Purpose
link, linkat
Link to a file. For more information, see the link(2) and linkat(2) man pages.
access, faccessat
Determine accessibility of a file. For more information, see the access(2) and faccessat(2) man pages.
mknod
Make a special or ordinary file. For more information, see the mknod(2) man page.
chmod, fchmodat
Change mode of file. For more information, see the chmod(2) and fchmodat(2)
chown, lchown, fchown, fchownat
Change owner and group of a file. For more information, see the chown(2), lchown(2), fchown(2), and fchownat(2) man pages.
utime
Set file access and modification times. For more information, see the utime(2) man page.
stat, lstat, fstat, fstatat
Get file status. For more information, see the stat(2), lstat(2), fstat(2), and fstatat(2) man pages.
fcntl
Perform file control functions. For more information, see the fcntl(2) man page.
ioctl
Control device. For more information, see the ioctl(2) man page.
fpathconf
Get configurable path name variables. For more information, see the fpathconf(2) man page.
opendir, readdir, closedir
Perform directory operations. For more information, see the opendir(3C), readdir(3C), and closedir(3C) man pages.
mkdir, mkdirat
Make a directory. For more information, see the mkdir(2) and mkdirat(2) man pages.
readlink, readlinkat
Read the value of a symbolic link. For more information, see the readlink(2) and readlinkat(2) man pages.
rename, renameat
Change the name of a file. For more information, see the rename(2) and renameat(2) man pages.
rmdir, unlinkat
Remove a directory. For more information, see the rmdir(2) and unlinkat(2) man pages.
symlink, symlinkat
Make a symbolic link to a file. For more information, see the symlink(2) and symlinkat(2) man pages.
fgetattr, fsetattr, getattrat, setattrat
Get and set system attributes. For more information, see the fgetattr(3C), fsetattr(3C), getattrat(3C), and setattrat(3C) man pages.

For more information, see syscall Provider in Oracle Solaris 11.4 DTrace (Dynamic Tracing) Guide.

File System Control

The following table lists the file system control interfaces that control the various aspects of the file system.

Table 3  File System Control Interfaces
Interface Name
Purpose
ustat
Get file system statistics. For more information, see the ustat(2) man page.
sync
Update super block. For more information, see the sync(2) man page.
mount
Mount a file system. For more information, see the mount(2) man page.
statvfs, fstatvfs
Get file system information. For more information, see the statvfs(2) and fstatvfs(2) man pages.
sysfs
Get file system type information. For more information, see the sysfs(2) man page.