1.实验 5.2.5 文件定位 用lseek()函数实现以下功能 1. 获取文件大小 2. 为文件添加指定长度的空洞 3. 在指定位置写入指定内容 4. 读出指定位置的内容 1. 获取文件大小
2017-07-14 23:11
1191 查看
1.实验 5.2.5 文件定位 用lseek()函数实现以下功能 1. 获取文件大小 2. 为文件添加指定长度的空洞 3. 在指定位置写入指定内容 4. 读出指定位置的内容 1. 获取文件大小 源代码: #include <stdio.h> #include <stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> int main(int argc,char *argv[]) { int fd,length; if(argc<2) { puts("Please input the open file pathnane!"); exit(1); } if((fd=open(argv[1],O_RDONLY))<0) { perror("Open file failure!"); exit(1); } if((length = lseek(fd,0,SEEK_END))<0) { perror("lseek file failure!"); } printf("The file's length is %d\n",length); close(fd); exit(0); } 2. 为文件添加指定长度的空洞 源代码: #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define ERR_EXIT(m) int main(void) { int fd; int ret; fd = open("tesk.txt",O_WRONLY|O_CREAT|O_TRUNC,0644); if(fd == -1) ERR_EXIT("open error"); write(fd,"hello",5); ret = lseek(fd, 4,SEEK_CUR); if(ret == -1) ERR_EXIT("lseek error"); write(fd,"world",5); close(fd); return 0; } 3. 在指定位置写入指定内容 源代码: #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<stdlib.h> char buf1[] = "1234567890"; char buf2[] = "ABCDEFGHIJ"; int main(int argc,char *argv[]) { int fd; if ( (fd = creat("/tmp/test",0644 )) < 0) { perror("creat"); exit(EXIT_FAILURE); } if (write(fd, buf1, 10) != 10) { perror("write"); exit(EXIT_FAILURE); } if(lseek(fd, 10, SEEK_SET) == -1)//偏移 { perror("lseek"); exit(EXIT_FAILURE); } if (write(fd, buf2, 10) != 10) { perror("write"); exit(EXIT_FAILURE); } return 0; } 4. 读出指定位置的内容 源代码: #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdio.h> #include<stdlib.h> #define MAXSIZE int main(void) { int i,fd,size,len; char *buf="Hello!hellohellohellohello"; char buf_r[10]; len = strlen(buf); if((fd = open("/tmp/hello.c", O_CREAT | O_TRUNC | O_RDWR,0666 ))<0){ perror("open:"); exit(1); } else printf("open file:hello.c %d\n",fd); if((size = write( fd, buf, len)) < 0){ perror("write:"); exit(1); } else printf("Write:%s\n",buf); lseek(fd, 0, SEEK_SET ); if((size = read( fd, buf_r, 10))<0){ perror("read:"); exit(1); } else printf("read form file:%s\n",buf_r); if( close(fd) < 0 ){ perror("close:"); exit(1); } else printf("Close hello.c\n"); exit(0); }
相关文章推荐
- php获取远程文件内容与大小的函数代码
- Installshield x:实现序列号检验,获取用户信息并写入指定_ini文件1 - 子夜 MySpace聚友博客
- python 将一个文件中内容添加到另一个文件指定位置
- 1.3 实现图片压缩、添加文字或图片水印、指定位置裁剪等功能的ImageUtility(内有效果图) (转)
- 用MFC实现代码获取当前EXE 执行的绝对路径,用于扩展相对路径扩展,以及计算文件大小功能
- Python 实现在对一个目录下所有文件,指定某一行之后添加内容(批处理脚本)
- 第十周实验报告(任务一)【由注释内容实现函数的功能】
- 编写一个类,该类具有统计文件长度,实现文件复制的函数。 其中复制功能函数如: void copy(string srcFileName,string targetFileName);
- 编写一个类,该类具有统计文件长度,实现文件复制的函数。 其中复制功能函数如: void copy(string srcFileName,string targetFileName);
- spark实现hive的合并输入很多小文件为指定大小的大文件的优化功能
- 在文件指定位置读取和写入内容RandomAccessFile
- XP中完美实现vista中快捷方式右键添加“打开文件位置”功能
- 从指定网址下载文件 并读出其内容 && 从网上下载文件放在本地&&从网上获取图片设在imageView里
- C# 从内嵌资源读取文件内容并写入到指定位置
- 实现在一个数组指定位置添加元素和删除元素的功能
- xcode4.2 模拟器定位 。xcode4.2 添加GPX文件。手工指定位置。
- Installshield x:实现序列号检验,获取用户信息并写入指定_ini文件1 - 子夜 MySpace聚友博客
- 在文件指定位置读取和写入内容RandomAccessFile
- java读写文件-经过多方搜索整合成我需要的功能(读写指定文件夹下面的多个指定名称的txt中的内容全部写入到一个指定的txt中)
- Linux的dd命令创建指定大小文件(空洞文件)的一种实现可能