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.mzph.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…