【學習筆記】Linux平台的檔案I/O操作

來源:互聯網
上載者:User

以下函數為不帶緩衝的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 。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.