Chapter4 File IO
4.1 Overview
File Descriptor = = Handle to Windows
Standard file Descriptor:
0 Standard input Stdin_filenostdin
1 Standard output Stdout_filenostdout
2 standard error Stderr_filenostderr
(1): FD = open (pathname, flags, mode)
(2): Numread = Read (fd, buffer, count)
(3): Numwritten = Write (fd, buffer, count)
(4): status = Close (FD)
4.2 General Purpose IO
4.3 Open a file: open ()
Open () opens a file that already exists, or you can create and open
#include <sys/stat.h>
#include <fcntl.h>
int open (const char* pathname, int flag, .../*mode_t mode*/);
Flag: A mask that specifies the access mode for the file.
Access mode of the file:
O_rdonly opening a file in read-only mode
O_wronly Open in write-only mode
O_rdwr Open in read-write mode
The value of the file descriptor returned by the open () Call:
SUSV3 specifies that if the call to open () succeeds, its return value must be guaranteed to be the least of the values in the file descriptor used by the process.
Eg: be sure to open the file using standard input (file descriptor 0).
if (Close (stdin_fileno) = =-1)
Errexit ("close");
FD = open (pathname, o_rdonly);
if (fd = =-1)
Errexit ("open");
The flags parameter in the 4.3.1 open () call
(1): File access mode, o_rdonly, O_WRONLY,O_RDWR flag, call Open () is not available at the same time. The F_GETFL operation of Fcntl () can retrieve the access mode of the file.
(2):
(3)
O_append: Always append data at the end of a file
O_async: When I/O operations are implemented for the file descriptor returned by the open () call, the system generates a signal notification process.
O_closexec: Enables the CLOSE-ON-FLAG flag (fd_cloexec) for the new file descriptor.
??
4.3.2 Open () function error
Open () returns 1, and the error number errno identifies the cause of the error if an error occurs opening the file.
Eacces: File permissions do not allow the calling process to open a file in a manner specified by the flags nibble
Eisdir: The specified file belongs to the directory, and the caller attempts to write.
Emfile: The number of files opened by the process reaches the set limit of process resources
Enfile: File Open resource reaches upper limit allowed by system
ENOENT: The file does not exist and the O_CREATE flag is not specified, or one of the specified path directories does not exist.
Erofs: File belongs to read-only file system
ETXTBSY: The specified file is an executable file and is running.
4.3.3 Create () system call
#include <fcntl.h>
int Create (cosnt char *pathname, mode_t mode);
The Create () system call is equivalent to the following open () call
FD = open (pathname, o_wronly | O_create | O_trunc. mode);
4.4 Read File contents: Read ()
#include <unistd.h>
ssize_t Read (int fd, void *buffer, size_t count);
Count: Maximum number of bytes to read
size_t: Unsigned shaping ssize_t: symbolic shaping
Note: The system call does not allocate a memory buffer for returning information to the caller.
Use Read () to read a sequence of characters from the terminal,
Char Buffer[max_read + 1];
Ssize-t Numread;
Numread = Read (Stdin_fileno, buffer, max_read) '
if (Numread = =-1)
Errexit ("read");
Buffer[numread] = ' + ';
printf ("The input data was:%s \ n", buffer);
4.5 Data written to file: Write ()
#include <unistd.h>
sszie_t Write (int fd, void *buffer, size_t count);
4.6 Closing files: Close ()
#inclue <unistd.h>
int close (int fd);
4.7 Changing the file offset: Lseek ()
#inclue <unistd.h>
off_t lseek (int fd, offst_t offset, int whence);
Offset: Specifies a numeric value in bytes.
Whence: Indicates that offset should be explained by reference to that base point
Seek_set: Starting from the head of the file
Seek_cur: Relative current file offset
Seek_end: File Tail
Eg: Gets the current position of the file offset Curr = Lseek (FD, 0, seek_cur);
File hole:
If the file offset crosses the end of the file and then performs an I/O operation, the read () call returns 0, indicating the end of the file. But the write () function can write data anywhere after the end of the file.
This space between the end of the file and the newly written data is called a file hole. From a programmatic point of view, there is a byte in the file hole, and the read hole will return a buffer populated with 0 (empty bytes).
4.8 Non-general purpose I/O Models: IOCTL ()
#include <sys/ioctl.h>
int ioctl (int fd, int request, .../* ARGP */)
"Linux_unix system Programming" CHAPTER4 file IO