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
原创粉丝点击