婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > Linux lseek函數(shù)的使用詳解

Linux lseek函數(shù)的使用詳解

熱門標(biāo)簽:大連電銷外呼系統(tǒng)運(yùn)營商 泰州智能外呼系統(tǒng)排名 接電話機(jī)器人罵人 長春電銷外呼系統(tǒng)代理商 百度地圖標(biāo)注尺寸無法顯示 400電話干嘛怎么申請信用卡 外呼系統(tǒng)虛擬號碼 400電話申請知乎 代理外呼系統(tǒng)創(chuàng)業(yè)

注:如果文章內(nèi)容有誤,請留言指出,謝謝合作。

名字

Name : lseek - reposition read/write file offset

lseek函數(shù)的作用是用來重新定位文件讀寫的位移。

頭文件以及函數(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的組合來決定:

SEEK_SET:
  從文件頭部開始偏移offset個(gè)字節(jié)。
SEEK_CUR:
  從文件當(dāng)前讀寫的指針位置開始,增加offset個(gè)字節(jié)的偏移量。
SEEK_END:
  文件偏移量設(shè)置為文件的大小加上偏移量字節(jié)。

測試代碼:

#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ù)來設(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);//注意,這里對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中。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:大慶 中衛(wèi) 清遠(yuǎn) 雅安 臺(tái)灣 長治 安陽 興安盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux lseek函數(shù)的使用詳解》,本文關(guān)鍵詞  Linux,lseek,函數(shù),的,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Linux lseek函數(shù)的使用詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Linux lseek函數(shù)的使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 五河县| 双牌县| 澄迈县| 玉山县| 自治县| 连州市| 白山市| 集安市| 兴义市| 绍兴市| 长治县| 吉林市| 九龙城区| 卫辉市| 南昌县| 荆州市| 朝阳区| 岑巩县| 胶州市| 伊金霍洛旗| 南通市| 黑河市| 平果县| 富民县| 洛南县| 商洛市| 和平县| 合江县| 呈贡县| 崇仁县| 梨树县| 米泉市| 罗源县| 罗甸县| 平利县| 宁国市| 贵溪市| 望都县| 昌江| 乐昌市| 湘潭市|