Linux lseek函數(shù)的使用詳解
注:如果文章內(nèi)容有誤,請(qǐng)留言指出,謝謝合作。
名字
Name : lseek - reposition read/write file offset
lseek函數(shù)的作用是用來(lái)重新定位文件讀寫的位移。
頭文件以及函數(shù)聲明
#include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence);
offset為正則向文件末尾移動(dòng)(向前移),為負(fù)數(shù)則向文件頭部(向后移)。
描述
lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence as follows:
SEEK_SET The file offset is set to offset bytes.
SEEK_CUR The file offset is set to its current location plus offset bytes.
SEEK_END The file offset is set to the size of the file plus offset bytes.
lseek() allows the file offset to be set beyond the end of the file (but this does not change the size of the file). If data is later written at this point, subsequent reads of the data in the gap (a “hole”) return null bytes (‘\0') until data is actually written into the gap.
lseek()函數(shù)會(huì)重新定位被打開文件的位移量,根據(jù)參數(shù)offset以及whence的組合來(lái)決定:
SEEK_SET:
從文件頭部開始偏移offset個(gè)字節(jié)。
SEEK_CUR:
從文件當(dāng)前讀寫的指針位置開始,增加offset個(gè)字節(jié)的偏移量。
SEEK_END:
文件偏移量設(shè)置為文件的大小加上偏移量字節(jié)。
測(cè)試代碼:
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #define BUFFER_SIZE 1024 #define SRC_FILE_NAME "src_file" #define DEST_FILE_NAME "dest_file" //根據(jù)傳入的參數(shù)來(lái)設(shè)置offset #define OFFSET (atoi(args[1])) int main(int argc, char*args[]) { int src_file, dest_file; unsigned char buff[BUFFER_SIZE]; int real_read_len, off_set; if (argc != 2) { fprintf(stderr, "Usage: %s offset\n", args[0]); exit(-1); } src_file = open(SRC_FILE_NAME, O_RDONLY); dest_file = open(DEST_FILE_NAME, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE );//owner權(quán)限:rw if (src_file < 0 || dest_file < 0) { fprintf(stderr, "Open file error!\n"); exit(1); } off_set = lseek(src_file, -OFFSET, SEEK_END);//注意,這里對(duì)offset取了相反數(shù) printf("lseek() reposisiton the file offset of src_file: %d\n", off_set); while((real_read_len = read(src_file, buff, sizeof(buff))) > 0) { write(dest_file, buff, real_read_len); } close(dest_file); close(src_file); return 0; }
結(jié)果解析
觀察offset以及dest_file和src_file文件的大小不難看出:程序通過lseek函數(shù)將src_file文件指針重新定位到文件末尾 + offset(注意,本程序?qū)ffset取了相反數(shù),即文件末尾 + (-offset))處,然后從文件末尾 + offset處開始向前復(fù)制文件到dest_file中。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:解決VMware安裝linux系統(tǒng)啟動(dòng)后黑屏問題
欄 目:Linux/apache
本文標(biāo)題:Linux lseek函數(shù)的使用詳解
本文地址:http://mengdiqiu.com.cn/a1/Linux_apache/10609.html
您可能感興趣的文章
- 04-02linux關(guān)閉串口命令 關(guān)閉linux端口命令
- 04-02linux文件命令重命名 linux重命名文件名命令
- 04-02linux命令注入過濾 linux 代碼注入
- 04-02linux中jobs命令 shell jobs命令
- 04-02linux依次執(zhí)行命令 linux命令的執(zhí)行過程是怎樣的?新手必讀
- 04-02linux命令免輸入 linux配置免密登錄
- 04-02linux命令注銷vnc linux命令行注銷用戶
- 04-02軟交換linux命令 軟交換網(wǎng)絡(luò)主要協(xié)議有哪些
- 04-02linux命令歷史記錄 linux查看歷史記錄的操作命令
- 04-02linux命令頁(yè)面 linux命令頁(yè)面中文


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02linux關(guān)閉串口命令 關(guān)閉linux端口命令
- 04-02linux文件命令重命名 linux重命名文件名
- 04-02linux中jobs命令 shell jobs命令
- 04-02linux命令注入過濾 linux 代碼注入
- 04-02linux依次執(zhí)行命令 linux命令的執(zhí)行過程
- 04-02linux命令注銷vnc linux命令行注銷用戶
- 04-02linux命令免輸入 linux配置免密登錄
- 04-02軟交換linux命令 軟交換網(wǎng)絡(luò)主要協(xié)議
- 04-02linux命令歷史記錄 linux查看歷史記錄的
- 04-02linux命令頁(yè)面 linux命令頁(yè)面中文
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載