以下函數為不帶緩衝的I/O(unbuffered I/O)函數
對於核心而言,所有開啟的檔案都由一個非負整數描述(稱為檔案描述符)。
下面介紹下檔案I/O函數:
(1)open
此函數為開啟或建立一個檔案。成功則返迴文件描述符;失敗則返回-1。
格式:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *name, int oflag[|optional parameter][,mode]);
(2)creat
此函數為建立一個新檔案。成功則返迴文件描述符;失敗則返回-1。
格式:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *name, mode_t mode);
(3)close
此函數為關閉一個已經打來的檔案。
格式:
#include <unistd.h>
int close(int file);
成功返回0,否則返回-1。
(4)lseek
此函數為制定一個當前檔案位移量。這個位移量是一個非負整數,用來說明從檔案開始處計算的位元組數。
格式:
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int file, off_t offset, int whence);
成功返迴文件位移;出錯返回-1。
(5)reed
此函數為從檔案中讀入資料。
格式:
#include <unistd.h>
size_t read(int file, void *buf, size_t bytes);
返回讀到的位元組書,若已讀到尾部則返回0,若出錯返回-1。
(6)write
此函數為向檔案中寫入資料。
格式:
#include <unistd.h>
size_t write(int file, void *buff, size_t bytes);
返回寫入檔案的位元組總數,大小一定小於緩衝區容量。出錯返回-1。
(7)dup
此函數可以用來複製一個現存的檔案描述符。
格式:
#include <unistd.h>
int dup(int oldfile);
int duo2(int oldfile, int newfile);//可以指定新檔案的描述符
成功返回次新檔案的檔案描述符;失敗返回-1 。
(8)fcntl
用途1:複製描述符(cmd=F_DUPED)
用途2:擷取/設定檔案愛你描述符標誌
用途3:擷取檔案狀態標誌
用途4:擷取/設定記錄鎖
用途5:擷取/設定非同步I/O
格式:
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(
int fd, int cmd, struct flock *lock);
(9)ioctl
此函數是input/output control 的縮寫,即輸入輸出控制。它可以通過一個檔案描述符來控制字元裝置。
格式:
#include <sys/ioctl.h>
int ioctl(int file, int request, char *argp|struct termios st);
成功返回0,出錯返回-1 。