//访问大文件
#define _LARGEFILE64_SOURCE
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"
#define EXIT_SUCESS 0
int
main(int argc, char *argv[])
{
int fd;
off64_t off;
if (argc != 3 || strcmp(argv[1],"--help") == 0)
usageErr("%s pathname offset\n",argv[0]);
fd = open64(argv[1],O_RDWR|O_CREAT,S_IRUSR | S_IWUSR);
if (fd == -1)
errExit("open64");
off = atoll(argv[2]);
if (lseek64(fd,off,SEEK_SET) == -1)
errExit("lseek64");
if (write(fd,"test",4) == -1)
errExit("write");
exit(EXIT_SUCESS);
}
/*
程序测试示例:
[root@localhost linux-test]# gl++ test.c
[root@localhost linux-test]# echo "hello world" > a.txt
[root@localhost linux-test]# cat a.txt
hello world
[root@localhost linux-test]# ./a.out a.txt "hello world"
[root@localhost linux-test]# cat a.txt
testo world
*/
访问大文件
最新推荐文章于 2024-03-24 23:34:31 发布
该程序演示了如何在Linux中打开、定位并写入大文件。通过`open64`、`lseek64`和`write`系统调用,实现了在指定偏移量处写入数据的功能。示例中,将字符串`test`写入到文件的特定位置,从而修改了文件内容。
384

被折叠的 条评论
为什么被折叠?



