lseek函數的使用

需要包含頭文件:<sys/types.h>? <unistd.h>

off_t lseek(int fd, off_t offset, int whence);? 函數原型

函數功能:移動文件讀寫指針;獲取文件長度;拓展文件空間。

在使用該函數之前需要將文件打開!? off_t 有符號整型 ??fd為文件描述符? offset參數指定偏移量?? whence參數指定具體從哪個位置開始偏移: SEEK_SET 文件頭? ?SEEK_CUR 當前指針位置?? SEEK_END 文件尾(注意文件尾為文件結束符EOF=-1)?

返回值:較文件起始位置向后的偏移量(到文件讀寫指針的位置) ?其可以大于當文件讀寫指針處于文件末尾時的偏移量,此時文件空間被拓展。

獲取文件長度: lseek( fd , 0 , SEEK_END);? 返回值即為文件長度。

?

//獲取一個文件的長度并且拓展該文件的空間

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>int main( )
{int fd;fd = open("file",O_WRONLY);   //打開file文件if( fd == -1 ){perror(" open file " );exit(1);}int length;length = lseek( fd , 0 , SEEK_END );  //獲取file文件的長度printf(" The length of the file is %d.\n",length);length=lseek( fd , 3000 , SEEK_END );  //拓展file文件空間,增加3000字節printf(" The length of the file is %d.\n",length);  //注意此時文件實質上還沒有被拓展,需要在末位寫入一些數據int fd1;fd1 = write( fd , "a" , 1);   //此時文件才被拓展,在文件末位寫入一個字節的數據if( fd1 == -1 ){perror(" write file " );exit(1);}length = lseek( fd , 0 , SEEK_END );printf(" The length of the file is %d, after lengthen.\n",length);int qw;qw = close(fd);if( qw == -1 ){perror( "close file");exit(1);}return 0;
}

[root@localhost work]# vim file

[root@localhost work]# ./rdwr

?The length of the file is 64.

?The length of the file is 3064.

?The length of the file is 3065, after lengthen.

?[root@localhost work]# vim rdwr.c

[root@localhost work]# ll file

-rwxrwxrwx. 1 root root 3065 Mar 19 16:54 file?? //最終文件大小為3065Bytes

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385402.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385402.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385402.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

19. 刪除鏈表的倒數第N個節點

給定一個鏈表&#xff0c;刪除鏈表的倒數第 n 個節點&#xff0c;并且返回鏈表的頭結點。 示例&#xff1a; 給定一個鏈表: 1->2->3->4->5, 和 n 2. 當刪除了倒數第二個節點后&#xff0c;鏈表變為 1->2->3->5. 說明&#xff1a; 給定的 n 保證是有效的。…

文件操作相關的系統函數

重點學習&#xff1a;stat&#xff08;fstat、lstat 獲取文件屬性&#xff09;、access&#xff08;測試指定文件是否擁有某種權限&#xff09;、chmod&#xff08;改變文件的權限&#xff09;、chown&#xff08;改變文件的所屬主和所屬組&#xff09;、truncate&#xff08;截…

stat函數(stat、fstat、lstat)

#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> //需包含頭文件 有如下三個函數的函數原型&#xff1a; int stat(const char *path, struct stat *buf); 第一個形參&#xff1a;指出文件&#xff08;文件路徑&#xff09;&…

1062. Talent and Virtue (25)

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about peoples talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a "sage&#xff08;圣人&#xff09;"…

access、strtol函數的使用(后者為C庫函數)

#include <unistd.h> int access(const char *pathname, int mode); 作用&#xff1a;檢查調用該函數的進程是否可以對指定的文件執行某種操作。 第一個形參&#xff1a;文件名&#xff1b;第二個形參&#xff1a;R_OK&#xff08;是否可讀&#xff09;、W_OK&#xf…

chmod、chown函數的使用

#include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode); 作用&#xff1a;改變指定文件的權限。第二個參數&#xff1a;mode必須為一個8進制數&#xff1b;返回值為0表示成功&#xff0c;-1表示失敗。 //代碼 #include…

606. 根據二叉樹創建字符串

你需要采用前序遍歷的方式&#xff0c;將一個二叉樹轉換成一個由括號和整數組成的字符串。 空節點則用一對空括號 "()" 表示。而且你需要省略所有不影響字符串與原始二叉樹之間的一對一映射關系的空括號對。 示例 1: 輸入: 二叉樹: [1,2,3,4] 1 / \ …

truncate、rename函數的使用

#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); 作用&#xff1a;用于拓展或截斷文件。將參數path 指定的文件大小改為參數length 指定的大小。如果原來的文件大小比參數le…

【Leetcode】112. 路徑總和

給定一個二叉樹和一個目標和&#xff0c;判斷該樹中是否存在根節點到葉子節點的路徑&#xff0c;這條路徑上所有節點值相加等于目標和。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹&#xff0c;以及目標和 sum 22&#xff0c; 5 / \ …

link、symlink、readlink、unlink函數的使用

#include <unistd.h> int link(const char *oldpath, const char *newpath); 作用&#xff1a;創建一個硬鏈接 0成功 -1 失敗 //代碼 #include <stdio.h> #include <stdlib.h> #include <unistd.h>int main(int argc, char* argv[]) {if(ar…

【Leetcode】113. 路徑總和 II

給定一個二叉樹和一個目標和&#xff0c;找到所有從根節點到葉子節點路徑總和等于給定目標和的路徑。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定如下二叉樹&#xff0c;以及目標和 sum 22&#xff0c; 5 / \ 4 8 / / \ …

目錄操作相關的系統函數

主要介紹幾個常用函數的使用方法&#xff1a;chdir&#xff08;改變進程的當前工作目錄&#xff09;、getcwd&#xff08;獲取當前進程的工作目錄&#xff09;、mkdir&#xff08;創建目錄&#xff09;、rmdir&#xff08;刪除空目錄&#xff09;、opendir&#xff08;打開一個…

1079. Total Sales of Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

chdir、getcwd、mkdir、rmdir函數

#include <unistd.h> int chdir(const char *path); int fchdir(int fd); 作用&#xff1a;改變調用這一函數的進程&#xff08;即程序執行&#xff09;的當前工作目錄&#xff0c;注意不是shell的當前工作目錄。 返回值&#xff1a;0成功 -1失敗 #include <unis…

【Leetcode | 235】 235. 二叉搜索樹的最近公共祖先

給定一個二叉搜索樹, 找到該樹中兩個指定節點的最近公共祖先。 百度百科中最近公共祖先的定義為&#xff1a;“對于有根樹 T 的兩個結點 p、q&#xff0c;最近公共祖先表示為一個結點 x&#xff0c;滿足 x 是 p、q 的祖先且 x 的深度盡可能大&#xff08;一個節點也可以是它自己…

1090. Highest Price in Supply Chain (25)

A supply chain is a network of retailers&#xff08;零售商&#xff09;, distributors&#xff08;經銷商&#xff09;, and suppliers&#xff08;供應商&#xff09;-- everyone involved in moving a product from supplier to customer. Starting from one root suppli…

opendir、readdir和closedir函數

注意&#xff1a;在Linux中&#xff0c;目錄的輸入格式&#xff1a;/mnt//fghs、/mnt/fghs、/mnt/fghs和/mnt/fghs//是等效的&#xff0c;都一樣。 #include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd); 返回…

146. LRU緩存機制

運用你所掌握的數據結構&#xff0c;設計和實現一個 LRU (最近最少使用) 緩存機制。它應該支持以下操作&#xff1a; 獲取數據 get 和 寫入數據 put 。 獲取數據 get(key) - 如果密鑰 (key) 存在于緩存中&#xff0c;則獲取密鑰的值&#xff08;總是正數&#xff09;&#xff…

dup和dup2函數

#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); 作用&#xff1a;dup函數實現對一個文件的文件描述符進行復制&#xff0c;復制之后該進程就會新增加一一個文件描述符指向該文件&#xff08;即實現同一個文件對應多個文件描述符&#xff0…

fcntl函數(網絡編程會用)

#include <unistd.h> #include <fcntl.h> int fcntl&#xff08;int fd, int cmd&#xff09;&#xff1b; int fcntl&#xff08;int fd, int cmd, long arg&#xff09;&#xff1b;//long 長整型 int fcntl&#xff08;int fd, int cmd, struct flock *lock…