Files that are organized as a sequence of data are called regular files. These can contain ASCII text, text in some other binary data encoding, executable code, or any combination of text, data, and code. The file has two components:
The control data, called the inode. These data include the file type, the access permissions, the owner, the file size, and the location(s) of the data blocks.
The file contents: a nonterminated sequence of bytes.
Solaris provides three basic forms of file input/output interfaces.
The traditional, raw, style of file I/O is described in "Basic File I/O".
The second form is the standard file I/O. The standard I/O buffering provides an easier interface and improved efficiency to an application run on a system without virtual memory. In an application running in a virtual memory environment, such as the Solaris 2.x system, standard file I/O is a very inefficient anachronism.
The third form of file I/O is provided by the memory mapping interface described in "Memory Management Interfaces ". Mapping files is the most efficient and powerful form of file I/O for most applications run in the Solaris 2.x environment.
The functions listed in Table 6-1 perform basic operations on files:
Table 6-1 Basic File I/O Functions
Function Name |
Purpose |
---|---|
open(2) |
Open a file for reading or writing |
close(2) |
Close a file descriptor |
read(2) |
Read from a file |
write(2) |
Write to a file |
creat(2) |
Create a new file or rewrite an existing one |
unlink(2) |
Remove a directory entry |
lseek(2) |
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(), by a return value of zero.
#include <fcntl.h> #define MAXSIZE 256 main() { int fd, n; char array[MAXSIZR] 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); }
Always close a file when you are done reading or writing it.
Offset into an open file are changed by read()s, write()s, or by calls to lseek(2). Some examples of using lseek() are:
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, (char *)&rec, sizeof (rec)); /* rewrite previous record */ n = lseek (fd, -sizeof (rec), SEEK_CUR); write (fd, (char *)&rec, sizeof (rec));
Advanced file I/O functions create and remove directories and files, create links to existing files, and obtain or modify file status information.
Table 6-2 Advanced File I/O Functions
Function Name |
Purpose |
---|---|
link(2) |
Link to a file |
access(2) |
Determine accessibility of a file |
mknod(2) |
Make a special or ordinary file |
chmod(2) |
Change mode of file |
chown(2), lchown, fchown |
Change owner and group of a file |
utime(2) |
Set file access and modification times |
stat(2), lstat, fstat |
Get file status |
fcntl(2) |
Perform file control functions |
ioctl(2) |
Control device |
fpathconf(2), pathconf |
Get configurable path name variables |
opendir readdir closedir |
Perform directory operations |
mkdir(2) |
Make a directory |
readlink(2) |
Read the value of a symbolic link |
rename(2) |
Change the name of a file |
rmdir(2) |
Remove a directory |
symlink(2) |
Make a symbolic link to a file |
File system control functions allow you to control various aspects of the file system:
Table 6-3 File System Control Functions
Function Name |
Purpose |
---|---|
ustat(2) |
Get file system statistics |
sync(2) |
Update super block |
mount(2) |
Mount a file system |
unmount |
Unmount a file system |
statvfs(2), fstatvfs |
Get file system information |
sysfs(2) |
Get file system type information |