LinuxC编程之IO-通过lseek对文件进行读写
来源:互联网 发布:unity3d制作的小游戏 编辑:程序博客网 时间:2024/05/17 00:13
1.相关API
通过lseek对文件进行读写
1)open函数
int open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);
2)read和write函数
#include <unistd.h>ssize_t read(int fd, void *buf, size_t count);ssize_t write(int fd, const void *buf, size_t count);3)lseek更改文件偏移位置
#include <sys/types.h>#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);4)把字符串转化成数字类型
头文件:#include <stdlib.h>
strtol() 函数用来将字符串转换为长整型数(long),其原型为:
long int strtol (const char* str, char** endptr, int base);
【参数说明】str 为要转换的字符串,endstr 为第一个不能转换的字符的指针,base 为字符串 str 所采用的进制。实现代码:
#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <string.h>#include <errno.h> //errno#include <fcntl.h>#include <ctype.h>#include <stdlib.h> //strtol()#include <unistd.h> //ssize_tint main(int argc, char *argv[]){ size_t len; off_t offset; int fd, ap, j; char *buf; ssize_t numRead, numWritten; if(argc < 3 || strcmp(argv[1], "--help") == 0) printf("%s file {r<length>| R<length> | w<string>| s<offset>}...\n", argv[0]); fd = open(argv[1], O_RDWR| O_CREAT, S_IRUSR| S_IWUSR| S_IRGRP | S_IWGRP | S_IROTH| S_IWOTH); if(fd == -1) { //打开文件出错 fprintf(stderr, "Open %s Error: %s\n", argv[1], strerror(errno)); exit(1); } for (ap = 2; ap < argc; ++ap) { switch (argv[ap][0]){ case 'r': //在当前offset偏移处显示文件内容 case 'R': len=strtol(&argv[ap][1], NULL, 0); buf = malloc(len); if(buf == NULL){ printf("malloc buf error\n"); exit(1); } numRead = read(fd, buf, len); if(numRead == -1){ printf("malloc buf error\n"); free(buf); exit(1); } if(numRead == 0){ printf("%s: end-of-file\n", argv[ap]); } else{ printf("%s: ",argv[ap]); for(j = 0; j< numRead; j++){ if (argv[ap][0]=='r'){ printf("%c", isprint((unsigned char)buf[j]) ? buf[j]: '?'); } else{ printf("%02x", (unsigned int) buf[j]); } } printf("\n"); } if(buf != NULL) free(buf); break; case 'w': //在当前文件偏移下写入内容 numWritten = write(fd, &argv[ap][1], strlen(&argv[ap][1])); if(numWritten == -1){ printf("write buf error\n"); exit(1); } printf("%s: wrote %ld bytes\n", argv[ap], (long) numWritten); break; case 's': //从文件头开始更改文件的偏移位置 offset = strtol(&argv[ap][1], NULL, 0); //offset = getLong(&argv[ap][1], GN_ANY_BASE, argv[ap]); if(lseek(fd, offset, SEEK_SET) == -1){ printf("lseek offset error\n"); exit(1); } printf("%s: seek successed\n", argv[ap]); break; default: printf("Argument must start with [rRws]\n"); break; } }//end for printf("file execute success\n");}运行结果:
0 0
- LinuxC编程之IO-通过lseek对文件进行读写
- Linux编程学习之文件IO篇-lseek函数
- 文件IO lseek
- LinuxC编程实战之文件操作
- 通过字节流来对文件进行读写操作
- 通过IO流对文件进行加密和解密
- IO对文件的读写
- 文件IO---lseek、fcntl函数
- 10---IO编程---文件读写
- Android对IO进行读写操作实例
- Linuxc编程之信号
- 28.对文件进行读写
- python对文件进行读写
- 对properties文件进行读写
- MFC之通过OLE实现对Excel文件的读写
- java中IO操作 利用文件流 过滤流对文件进行读写操作
- Unix学习笔记------文件IO------使用lseek设置文件当前读写偏移量(即:当前的读写位置)
- Linux文件编程之【系统调用】—— lseek()
- nyoj 115 城市平乱(spfa)
- 拦截导弹 dp java
- Linux信号1
- socket网络编程
- 在编译android4.4的时候libwebviewchromium.so时停止很长时间发生错误
- LinuxC编程之IO-通过lseek对文件进行读写
- 最长回文子串(Manacher算法)
- 深度学习中momentum的作用
- c++第四次作业
- vue-cli&webpack&arcgis API For JS的天坑之路(一)
- React 入门实例教程
- label的用法
- Linux下mysql集群搭建
- MongoDB 工具类及简单测试